PHP Array Functions: Iterator Functions in PHP
By Nihar Ranjan Das, Published on June 15th 2023 | 8 mins, 765 words
In PHP, An array is an ordered map. A map is a data structure that stores a collection of key-value pairs where each key associated with a value. In PHP, array stores key-value pairs.
Array are very important data structure in PHP. We can store a large amount of data in a array. Sometimes, we need to manipulate the data of a array. And for this, there are so many build in functions in PHP.
In this article, we will learn how to manipulate array data in PHP. Let's dive deep and explore them.
Array Functions
- array_map
- array_filter
- array_reduce
- array_walk
array_map
The array_map function returns an array besed on the instraction of the callback. The number of parameters of the callback need to be passed in the array_map otherwise an ArgumentCountError will be thrown.
Parameters
- callback: A callable function will access each items in the each array
- array: A array to customise through callback
- arrays: If we need to pass multiple parameters through callback then need to pass those number of parameters also after callback in array_map function.
Return
Returns an new array besed on the instraction of the callback applied to the provided array.
Example
data = [ [ "name" => "John", "admin" => false ], [ "name" => "Mary", "admin" => true ], [ "name" => "Peter", "admin" => false ], [ "name" => "Tony", "admin" => true ] ]; // Get the array of admins and add a column named avatar $admins = array_map(function ($name) { if ($name['admin']) { $name['avatar'] = $name['name'][0]; } }, $data); $admins = array_filter($admins); print_r($admins); /* Output Array ( [1] => Array ( [name] => Mary [admin] => 1 [avatar] => M ) [3] => Array ( [name] => Tony [admin] => 1 [avatar] => T ) ) */
array_filter
The array_map function returns an array by filtering elements of an array using the callback. If the callback function returns true, then the current value of the array returns as a new array.
Parameters
- array: A array to filter by the callback
- callback: A callable function will iterare each items of the array
- mode: flag that indicates what kind of data are send to callback. This is a optional parameter. ARRAY_FILTER_USE_KEY - pass key as the only argument to callback instead of the value. ARRAY_FILTER_USE_BOTH - pass both value and key as arguments to callback instead of the value. Default is 0 which will pass value as the only argument to callback instead.
Return
Returns an new array filtered by callback
Example
$data = [ [ "name" => "John", "admin" => false ], [ "name" => "Mary", "admin" => true ], [ "name" => "Peter", "admin" => false ], [ "name" => "Tony", "admin" => true ] ]; // Get the array of admins $admins = array_filter($data, function ($value, $key) { return $value['admin']; }, ARRAY_FILTER_USE_BOTH); print_r($admins); /* Output Array ( [1] => Array ( [name] => Mary [admin] => 1 ) [3] => Array ( [name] => Tony [admin] => 1 ) ) */
array_reduce
The array_reduce function reduce an array to a single value using the callback.
Parameters
- array: A array to reduce by the callback
- callback: A callable function will access each items in the each array. The callback function has two parameters. carray (value of the previous iteration) & item (the value of current item of the array).
- initial: This is an optional parameter.
Return
Return a value after reducing the array.
Example
$data = [ [ "name" => "John", "salary" => 3000 ], [ "name" => "Mary", "salary" => 2500 ], [ "name" => "Peter", "salary" => 2700 ], [ "name" => "Tony", "salary" => 3500 ] ]; // Get the array of admins and add a column named avatar $totalSalary = array_reduce($data, function ($carry, $item) { return $carry + $item['salary']; }, 0); echo $totalSalary; /* Output 11700 */
array_walk
The array_walk function applies a user defined function to every item of an array.
Parameters
- array: A array to iterate
- callback: A callable function will be applied to each items in the each array. The callback function has two parameters. item (the value of current item of the array) & key (index of current item).
- arg: This is an optional parameter. If any optional parameter is supplied, it will be passed as the third parameter to the callback.
Return
Return true.
Example
$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple"); function test_alter(&$item1, $key, $prefix) { $item1 = "$prefix: $item1"; } function test_print($item2, $key) { echo "$key. $item2\n"; } echo "Before ...:\n"; array_walk($fruits, 'test_print'); array_walk($fruits, 'test_alter', 'fruit'); echo "... and after:\n"; array_walk($fruits, 'test_print'); /* Output Before ...: d. lemon a. orange b. banana c. apple ... and after: d. fruit: lemon a. fruit: orange b. fruit: banana c. fruit: apple */