PHP Array Functions
PHP array_change_key_case() Function +
The array_change_key_case() function changes all keys in an array to lowercase or uppercase.
Syntax:
array_change_key_case(array,case);
CASE_LOWER - Default value. Changes the keys to lowercase
CASE_UPPER - Changes the keys to uppercase
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43"); $age= array_change_key_case($age,CASE_UPPER); foreach ( $age as $key=>$value){ echo $key. " , "; } The output will be: peter, ben, joe --------------------------------------------------------------- If two or more keys will be equal after running array_change_key_case() (e.g. "b" and "B"), the latest array will override the other: $pets=array("a"=>"Cat","B"=>"Dog","c"=>"Horse","b"=>"Bird"); print_r(array_change_key_case($pets,CASE_UPPER)); Array ( [A] => Cat [B] => Bird [C] => Horse
PHP array_count_values() Function +
The array_count_values() function counts all the values of an array.
Return Value: Returns an associative array, where the keys are the original array's values, and the values are the number of occurrences
$a=array("A","Cat","Dog","A","Dog"); print_r(array_count_values($a)); The output will be: Array ( [A] => 2 [Cat] => 1 [Dog] => 2 )
PHP array_key_exists() Function +
The array_key_exists() function checks an array for a specified key, and returns true if the key exists and false if the key does not exist.
Tip: Remember that if you skip the key when you specify an array, an integer key is generated, starting at 0 and increases by 1 for each value.
$a=array("Volvo"=>"XC90","BMW"=>"X5"); if (array_key_exists("Volvo",$a)) { echo "Key exists!"; } else { echo "Key does not exist!"; } ?> The output will be: Key exists! ------------------------------------------ Check if the integer key "0" exists in an array: $a=array("Volvo","BMW"); if (array_key_exists(0,$a)) { echo "Key exists!"; } else { echo "Key does not exist!"; } The output will be: Key exists!
PHP array_keys() Function +
Return an array containing the keys:
$a=array("Volvo"=>"XC90","BMW"=>"X5","Toyota"=>"Highlander"); print_r(array_keys($a));
Syntax:
array_keys(array,value,strict)
$a=array("Volvo"=>"XC90","BMW"=>"X5","Toyota"=>"Highlander"); $b=array_keys($a); foreach ( $b as $value){ echo $value ." , "; } The output will be: Volvo , BMW , Toyota ,
PHP array_map() Function +
The array_map() function sends each value of an array to a user-made function, and returns an array with new values, given by the user-made function.
Tip: You can assign one array to the function, or as many as you like.
Syntax:
array_map(myfunction,array1,array2,array3...)
function myfunction($v) { if ($v==="Dog") { return "Fido"; } return $v; } $a=array("Horse","Dog","Cat"); print_r(array_map("myfunction",$a)); The output will be: Array ( [0] => Horse [1] => Fido [2] => Cat ) ------------------------------------------- Using two arrays: function myfunction($v1,$v2) { if ($v1===$v2) { return "same"; } return "different"; } $a1=array("Horse","Dog","Cat"); $a2=array("Cow","Dog","Rat"); print_r(array_map("myfunction",$a1,$a2)); The output will be: Array ( [0] => different [1] => same [2] => different ) ------------------------------------------------------- Change all letters of the array values to uppercase: function myfunction($v) { $v=strtoupper($v); return $v; } $a=array("Animal" => "horse", "Type" => "mammal"); print_r(array_map("myfunction",$a)); The output will be: Array ( [Animal] => HORSE [Type] => MAMMAL --------------------------------------------------- Assign null as the function name $a1=array("Dog","Cat"); $a2=array("Puppy","Kitten"); print_r(array_map(null,$a1,$a2));
PHP array_merge() Function +
Definition and Usage:
The array_merge() function merges one or more arrays into one array.
Tip: You can assign one array to the function, or as many as you like.
Note: If two or more array elements have the same key, the last one overrides the others.
Note: If you assign only one array to the array_merge() function, and the keys are integers, the function returns a new array with integer keys starting at 0 and increases by 1 for each value (See Example 1 below).
Tip:
The difference between this function and the array_merge_recursive() function is when two or more array elements have the same key. Instead of override the keys, the array_merge_recursive() function makes the value as an array.Syntax:
array_merge(array1,array2,array3...)
$a1=array("red","green"); $a2=array("blue","yellow"); print_r(array_merge($a1,$a2)); The output will be: Array ( [0] => red [1] => green [2] => blue [3] => yellow ) ----------------------------------------------------------------- Merge two associative arrays into one array: $a1=array("a"=>"red","b"=>"green"); $a2=array("c"=>"blue","b"=>"yellow"); print_r(array_merge($a1,$a2)); The output will be: Array ( [a] => red [b] => yellow [c] => blue ) ------------------------------------------------------------ Using only one array parameter with integer keys: $a=array(3=>"red",4=>"green"); print_r(array_merge($a)); The output will be: Array ( [0] => red [1] => green )
PHP array_multisort() Function +
Definition and Usage:
The array_multisort() function returns a sorted array. You can assign one or more arrays.
The function sorts the first array, and the other arrays follow, then, if two or more values are the same, it sorts the next array, and so on.
Note: String keys will be maintained, but numeric keys will be re-indexed, starting at 0 and increase by 1.
Note: You can assign the sorting order and the sorting type parameters after each array.
If not specified, each array parameter uses the default values.array_multisort(array1,sorting order,sorting type,array2,array3...)
sorting order Optional. Specifies the sorting order. Possible values:
SORT_ASC - Default. Sort in ascending order (A-Z) SORT_DESC - Sort in descending order (Z-A) $a=array("Dog","Cat","Horse","Bear","Zebra"); array_multisort($a); print_r($a); The output will be: Array ( [0] => Bear [1] => Cat [2] => Dog [3] => Horse [4] => Zebra )
PHP array_pop() Function +
The array_pop() function deletes the last element of an array.
Syntax:
array_pop(array)
Delete the last element of an array:
$a=array("red","green","blue"); array_pop($a); print_r($a); The ouput will be: Array ( [0] => red [1] => green ) Return Value: Returns the last value of array. If array is empty, or is not an array, NULL will be returned.
PHP array_push() Function +
Syntax:
array_push(array,value1,value2...)
The array_push() function inserts one or more elements to the end of an array.
Tip: You can add one value, or as many as you like.
Note: Even if your array has string keys, your added elements will always have numeric keys (See example below).
$a=array("red","green"); array_push($a,"blue","yellow"); print_r($a); The output will be: Array ( [0] => red [1] => green [2] => blue [3] => yellow ) -------------------------------------------------------------- $a=array("a"=>"red","b"=>"green"); array_push($a,"blue","yellow"); print_r($a); The output will be: Array ( [a] => red [b] => green [0] => blue [1] => yellow )
PHP count() Function +
The count() function returns the number of elements in an array.
Syntax:
count(array,mode);
mode Optional. Specifies the mode. Possible values: 0 - Default. Does not count all elements of multidimensional arrays 1 - Counts the array recursively (counts all the elements of multidimensional arrays) $cars=array( "Volvo"=>array ( "XC60", "XC90" ), "BMW"=>array ( "X3", "X5" ), "Toyota"=>array ( "Highlander" ) ); echo "Normal count: " . count($cars); echo "Recursive count: " . count($cars,1); The output will be: Normal count: 3 Recursive count: 8
PHP current() Function +
The current() function returns the value of the current element in an array.
Every array has an internal pointer to its "current" element, which is initialized to the first element inserted into the array.
Tip: This function does not move the arrays internal pointer.
Related methods:
- moves the internal pointer to, and outputs, the last element in the array
- moves the internal pointer to, and outputs, the next element in the array
- moves the internal pointer to, and outputs, the previous element in the array
- moves the internal pointer to the first element of the array
- returns the current element key and value, and moves the internal pointer forward
$people = array("Peter", "Joe", "Glenn", "Cleveland"); echo current($people) ; The output will be: Peter
The end() function moves the internal pointer to, and outputs, the last element in the array.
$people = array("Peter", "Joe", "Glenn", "Cleveland"); echo current($people); echo end($people); The output will be: Peter Cleveland ---------------------------------------------------------------------- The next() function moves the internal pointer to, and outputs, the next element in the array. $people = array("Peter", "Joe", "Glenn", "Cleveland"); echo current($people); echo next($people); The output will be: Peter Joe -------------------------------------------------------------------- $people = array("Peter", "Joe", "Glenn", "Cleveland"); echo current($people) // The current element is Peter echo next($people) // The next element of Peter is Joe echo current($people) // Now the current element is Joe echo prev($people) // The previous element of Joe is Peter echo end($people) // The last element is Cleveland echo prev($people) // The previous element of Cleveland is Glenn echo current($people) // Now the current element is Glenn echo reset($people) // Moves the internal pointer to the first element of the array, which is Peter echo next($people) // The next element of Peter is Joe print_r (each($people)); // Returns the key and value of the current element (now Joe), and moves the internal pointer forward ------------------------------------------------------------------------------- each() is typically used in conjunction with list() to traverse an array, here's an example: $fruit = array('a' => 'apple', 'b' => 'banana', 'c' => 'cranberry'); reset($fruit); while (list($key, $val) = each($fruit)) { echo "$key => $val\n"; } The output will be: a => apple b => banana c => cranberry ---------------------------------------------------------- Caution: Because assigning an array to another variable resets the original arrays pointer, our example above would cause an endless loop had we assigned $fruit to another variable inside the loop. Warning: each() will also accept objects, but may return unexpected results. Its therefore not recommended to iterate though object properties with each().
PHP list() Function +
The list() function is used to assign values to a list of variables in one operation.
Note: This function only works on numerical arrays.
Syntax:
list(var1,var2...)
Assign variables as if they were an array:
$my_array = array("Dog","Cat","Horse"); list($a, $b, $c) = $my_array; echo "I have several animals, a $a, a $b and a $c."; The output will be: I have several animals, a Dog, a Cat and a Horse. ------------------------------------------------------------ Using the first and third variables: $my_array = array("Dog","Cat","Horse"); list($a, , $c) = $my_array; echo "Here I only use the $a and $c variables."; The output will be: Here I only use the Dog and Horse variables.
PHP in_array() Function +
The in_array() function searches an array for a specific value.
Note: If the search parameter is a string and the type parameter is set to TRUE, the search is case-sensitive.
$people = array("Peter", "Joe", "Glenn", "Cleveland"); if (in_array("Glenn", $people)) { echo "Match found"; } else { echo "Match not found"; } The output will be: Match found