There are always some special needs, so I found this function.
The following is excerpted from the official manual array_map()
callback -- callback function, applied to each each element in the array.
array1 -- Array, traverse and run the callback function.
Array list, each traverses and runs the callback function.
array_map -- Apply a callback function to each element of the array
array array_map ( callable $callback , array $array1 [, array $ ... ] )
array_map(): Returns an array, which is the array after applying the callback function to each element of array1. The number of callback function parameters and the number of arrays passed to array_map() must be the same.
Parameters
Return value -- Returns an array containing all elements of array1 after the callback function is processed.
Example
##
<?php $arr = [ ['a' => 'aa','b' => 'bb',], ['c' => 'cc','d' => 'dd',], ['e' => 'ee','f' => 'ff',], ]; function test($v){ $v['add'] = 0; return $v; } $arr = array_map("test",$arr); print_r($arr);?>
Array( [0] => Array ( [a] => aa [b] => bb [add] => 0 ) [1] => Array ( [c] => cc [d] => dd [add] => 0 ) [2] => Array ( [e] => ee [f] => ff [add] => 0 ))
<?php namespace User\Controller; use Common\Controller\ManagerController; class DataController extends Controller { public function get_data() { $arr = [ // 数据填充 ]; $arr = array_map([$this,'_add_param'],$arr); dump($arr); } private function _add_param($value){ $value['add'] = 'xxx'; return $value; } }
The above is the detailed content of Regarding the use of functions such as array_map() in the PHP framework. For more information, please follow other related articles on the PHP Chinese website!