Tutorial by Examples

To apply a function to every item in an array, use array_map(). This will return a new array. $array = array(1,2,3,4,5); //each array item is iterated over and gets stored in the function parameter. $newArray = array_map(function($item) { return $item + 1; }, $array); $newArray now is a...
array_chunk() splits an array into chunks Let's say we've following single dimensional array, $input_array = array('a', 'b', 'c', 'd', 'e'); Now using array_chunk() on above PHP array, $output_array = array_chunk($input_array, 2); Above code will make chunks of 2 array elements and create a...
implode() combines all the array values but looses all the key info: $arr = ['a' => "AA", 'b' => "BB", 'c' => "CC"]; echo implode(" ", $arr); // AA BB CC Imploding keys can be done using array_keys() call: $arr = ['a' => "AA", 'b'...
array_reduce reduces array into a single value. Basically, The array_reduce will go through every item with the result from last iteration and produce new value to the next iteration. Usage: array_reduce ($array, function($carry, $item){...}, $defaul_value_of_first_carry) $carry is the result f...
Use list() to quick assign a list of variable values into an array. See also compact() // Assigns to $a, $b and $c the values of their respective array elements in $array with keys numbered from zero list($a, $b, $c) = $array; With PHP 7.1 (currently in beta) you will be able to use s...
There are two ways to push an element to an array: array_push and $array[] = The array_push is used like this: $array = [1,2,3]; $newArraySize = array_push($array, 5, 6); // The method returns the new size of the array print_r($array); // Array is passed by reference, therefore the original ar...

Page 1 of 1