Home > Article > Backend Development > Examples of how to use array_map() and other functions in the php framework
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 callback function processing.
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);?>Output result
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 Examples of how to use array_map() and other functions in the php framework. For more information, please follow other related articles on the PHP Chinese website!