Obtaining Array Keys and Values : Associate Array « Data Structure « PHP






Obtaining Array Keys and Values

 
<?php 

function array_display($array, $pre=FALSE){ 
    $tag = $pre ? 'pre' : 'p'; 
    printf("<%s>%s</%s>\n", $tag, var_export($array, TRUE), $tag); 
} 

$fruits = array('red' => 'apple', 'yellow' => 'banana', 'green' => 'lime'); 

$colors = array_keys($fruits); 
$flavors = array_values($fruits); 


array_display($fruits); 
array_display($colors); 
array_display($flavors); 
?>
  
  








Related examples in the same category

1.Use foreach, while and list to loop through associate array
2.Sort associate array
3.array_key_exists
4.array_values: Return all the values of an array
5.array_keys: Return all the keys of an array
6.Arrays Using Associative Array Notation
7.Reference the assiciate array element
8.foreach statement is used to loop through an associative array
9.Associative Arrays
10.Looping Through an Associative Array with foreach
11.key: Fetch a key from an associative array
12.print_r: output associate array
13.array_intersect_assoc: Computes the intersection of arrays with additional index check
14.An array called $computers with numeric and string keys
15.An array called $vegetables with string keys
16.Argument Swapping
17.Array to object
18.Assigning and Comparing
19.Building an array of defaults
20.Creates an array with keys 'a', 'b' and 'c with values of 1, 2 and 3 as well as keys 0, 1 and 2 with values 'a', 'b', and 'c'
21.Creating Arrays
22.Creates an array that assigns keys 1 through 7 to the days of the week.
23.Creating a multidimensional array
24.Creating an associative array of shapes
25.Obtaining Array Keys with a Given Value
26.One-based array index
27.Displaying one value from an array
28.Using both the array key and a passed-in value to modify each element value
29.Using Variable Variables to Create Associations