How to merge multi-dimensional arrays in php

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.