The array submitted by the first group of checkpoints is as follows: array (& #039; sort & #039; & amp; gt; array )) the array submitted by the second group of checkpoints is as follows: array (& #039; id & #039; & amp; gt; array )) how to process the following array result array (& #039; sort & #039; & amp; gt; 0, & #039; id & #039; & amp; gt; 1), array (& #039; sort... the array submitted by the first group of checkpoints is as follows:
Array ('sort '=> array ))
The array submitted by the second group of checkpoints is as follows:
Array ('id' => array (1, 2, 3, 4 ))
How to process the result in the following Array
Array (
Array ('sort '=> 0, 'id' => 1 ),
Array ('sort '=> 2, 'id' => 2 ),
Array ('sort '=> 3, 'id' => 3 ),
Array ('sort '=> 1, 'id' => 4 ),
)
Reply content:
The array submitted by the first group of checkpoints is as follows:
Array ('sort '=> array ))
The array submitted by the second group of checkpoints is as follows:
Array ('id' => array (1, 2, 3, 4 ))
How to process the result in the following Array
Array (
Array ('sort '=> 0, 'id' => 1 ),
Array ('sort '=> 2, 'id' => 2 ),
Array ('sort '=> 3, 'id' => 3 ),
Array ('sort '=> 1, 'id' => 4 ),
)
$res = array_map(function($v1,$v2){return ['sort'=>$v1,'id'=>$v2];},$a['sort'],$b['id']);/* [ [ "sort" => 0, "id" => 1 ], [ "sort" => 2, "id" => 2 ], [ "sort" => 3, "id" => 3 ], [ "sort" => 1, "id" => 4 ] ]*/
The answer above is very good. Using array_map is indeed the best method, but the syntax is not compatible with earlier versions. I wrote a complete and can be directly tested.
array(0,2,3,1)); $b = array('id'=>array(1,2,3,4)); $res = array_map(function($v1,$v2){ return array('sort'=>$v1,'id'=>$v2); },$a['sort'],$b['id']); print_r($res);
Result
Array( [0] => Array ( [sort] => 0 [id] => 1 ) [1] => Array ( [sort] => 2 [id] => 2 ) [2] => Array ( [sort] => 3 [id] => 3 ) [3] => Array ( [sort] => 1 [id] => 4 ))
array(0,2,3,1));$arr2 = array('id' => array(1,2,3,4));$arr = array_merge($arr1, $arr2);$new = array();foreach($arr as $k => $v) { foreach($v as $k1 => $v1) { $new[$k1][$k] = $v1; }}print_r($new);