There are 2 arrays {code...} When I output array1 cyclically. How do I replace uid with the name in array2? {Code...} has two Arrays
$ Array1 = [[0] => array (3) {["id"] => "1", ["uid"] => '123 ', ["status"] => '0' [1] => array (3) {["id"] => "2 ", ["uid"] => '000000', ["status"] => '1'] $ array2 = [[0] => array (2) {["uid"] => "123", ["name"] => 'zhang san' [1] => array (2) {["uid"] => "321", ["name"] => 'Li si']
When I output array1 cyclically. How do I replace uid with the name in array2?
Foreach ($ array1 as $ item) {$ item ['status'] = $ item ['status'] = 1? 'Yes': 'no'; $ item ['uid'] = xxx; // how do I replace it with name? $ Newarr [] = $ item ;}
Reply content:
There are 2 Arrays
$ Array1 = [[0] => array (3) {["id"] => "1", ["uid"] => '123 ', ["status"] => '0' [1] => array (3) {["id"] => "2 ", ["uid"] => '000000', ["status"] => '1'] $ array2 = [[0] => array (2) {["uid"] => "123", ["name"] => 'zhang san' [1] => array (2) {["uid"] => "321", ["name"] => 'Li si']
When I output array1 cyclically. How do I replace uid with the name in array2?
Foreach ($ array1 as $ item) {$ item ['status'] = $ item ['status'] = 1? 'Yes': 'no'; $ item ['uid'] = xxx; // how do I replace it with name? $ Newarr [] = $ item ;}
Are the locations of matching data of the two arrays one-to-one matched? If so, it is relatively simple:
$count = count($array1);for ($i = 0; $i < $count; ++$i) { $array1[$i]['uid'] = $array2[$i]['name'];}
In this case, foreach is not convenient.
If the data does not match and needs to be searched in array2, a simple example is provided:
foreach ($array1 as &$item) { $item['uid'] = get_name($array2, $item['uid']);}function get_name(&$data, $uid){ foreach ($data as &$item) { if ($item['uid'] == $uid) { $name = $item['name'] unset($item); return $name; } }}
Further expansion
$array3 = function() use($array2) { $res = array(); foreach ($array2 as $item) { $res[$item['uid']] = $item['name']; } return $res;};foreach ($array1 as &$item) { $item['uid'] = $array3[$item['uid']];}
$array2 = array_column($array2, 'name', 'uid');foreach($array1 as &$arr) $arr['uid'] = $array2[$arr['uid']];
No. How can I change my mind?
Http://php.net/manual/en/function.array-merge-recursive.php