Índice

Array Basics

$array = array(1,2,3);
print_r($array);    // Array ( [0] => 1 [1] => 2 [2] => 3 )
var_dump($array);   // array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) }
var_export($array); // array ( 0 => 1, 1 => 2, 2 => 3, )

 

Creating Arrays

Comparing Arrays

Iteration

while (key ($array) !== null) {  // != is incorrect as key may be 0
echo key($array) .": " .current($array) . PHP_EOL;
next($array);
}

Re-ordering and Sorting

Stacks & Queues – original array is changed

Array Functions

array_slice ( array $array , int $offset [, int $length = NULL [, bool $preserve_keys = false ]] )
– return the sliced array, the original array is untouched
– $offset element is counted from 0
– if $length is omitted, will contain elements till the end
– negative $offset means element position counted from the end (not including this one)
– if $preserve_keys == TRUE, numeric keys will be preserved, associative keys will always be preserved

array_splice ( array &$input , int $offset [, int $length = 0 [, mixed $replacement ]] )
– cut out a chunk of an array and replace with new elements (if supplied) and return the extracted values
– $offset element is included
– negative $offset means element position counted from the end (not including this one)

$input = array("red", "green", "blue", "yellow");
array_splice($input, 2);
// $input is now array("red", "green")

$input = array("red", "green", "blue", "yellow");
array_splice($input, -1, 1, array("black", "maroon"));
// $input is now array("red", "green", "blue", "black", "maroon")

$input = array("red", "green", "blue", "yellow");
array_splice($input, 3, 0, "purple");
// $input is now array("red", "green", "blue", "purple", "yellow");

array_merge ( array $array1 , array $array2, … )
– associative key values preserved; numeric key values renumbered
– If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.
– note: $array1 + $array2 The keys from the first array will be preserved. If an array key exists in both arrays, then the element from the first array will be used and the matching key’s element from the second array will be ignored.

array_merge_recursive ( array $array1 , array $array2, … )
– If the input arrays have the same string keys, then the values for these keys are merged together into an array
– If, however, the arrays have the same numeric key, the later value will not overwrite the original value, but will be appended.

array_map ( callable func() name[‘strtoupper’] , array $array1, array $array2, … )
– the callback function should contain the same number of arguments as the number of arrays
– return an array with all values return by the callback function
– if the callback is NULL, the arrays will be combined with $array[0][0]=$array1[0], $array[0][1]=$array2[0] and so on
– keys not preserved

array_replace ( array $array1 , array $array2, … )
– replace or add key/value pairs to $array1, the same keys will overwrite the value, while different keys will be added

array_replace_recursive ( array $array1 , array $array2, … )
– array_replace recursively

array_combine ( array $keys , array $values )
– create a new array, first array becomes keys, second array becomes values
– FALSE if number of elements in $keys and $values are not equal

array_unique ( array $array1 , $sort_flags )
– compare the values, only the first unique value together with its key will be shown, by default, 4 “4” are considered the same

array_change_key_case($input_array, CASE_UPPER|CASE_LOWER)
– copy and return the $input_array with the case of all keys changed

array_count_values($array)
– copy and return an associative array with all the values of $array as keys and counts of the values as value

array_reduce ( array $array, callable func(), $initial_value )
– reduce the array to a single value based on the callback function

array_sum ( array $array )
– return the sum of the values in an array (with type casted to float)

array_product ( array $array )
– return the product of the values in an array (with type casted to float)

compact ( varname, varname, … )
– creates an array containing variables and their values
– opposite of extract

extract ( array &$var_array [, int $extract_type = EXTR_OVERWRITE [, string $prefix = NULL ]])
– turn all key/value pairs of an associative array into variables with the values assigned
– e.g. $arr = array(“a”=>”b”); extract($arr); // result: $a=b

SPL, Objects as arrays