php将两个数组相同的key合并到一个数组

WBOY
Release: 2016-06-06 20:46:07
Original
1482 people have browsed it

<code class="lang-php">$arr = array(
    array(
    'id' => 1,
    'user_name'=>'test1'
    ),
    array(
    'id' => 2,
    'user_name'=>'test2'
    ),
    array(
    'id' => 3,
    'user_name'=>'test3'
    )
);
$arr2 = array(
     array(
    'id' => 1,
    'shop_name'=>'shop1'
    ),
    array(
    'id' => 5,
    'shop_name'=>'shop2'
    ),
    array(
    'id' => 3,
    'shop_name'=>'shop3'
    )
);
怎么根据2个数组id相同的一维数组,将$arr2的shop_name添加到$arr,如果没有相同的id,shop_name为空,形成如下数组:
$good_arr =array(
    array(
    'id' => 1,
    'user_name'=>'test1',
    'shop_name'=>'shop1'
    ),
    array(
    'id' => 2,
    'user_name'=>'test2',
    'shop_name'=>''
    ),
    array(
    'id' => 3,
    'user_name'=>'test3',
    'shop_name'=>'shop3'
    )

);

</code>
Copy after login
Copy after login

我的方案:有更好的吗?O(∩_∩)O谢谢

<code class="lang-php">    /*foreach($arr as $key => &$value) {
        $value['shop_name']='';
    }
        unset( $value );
        */
    foreach($arr as $k2 =>&$v2) {
            $v2['shop_name']='';
        foreach($arr2 as $k =>$v) {
            if($v2['id'] == $v['id']) {
            $v2['shop_name'] = $v['shop_name'];
            }
        }
    }
    print_r($arr);

</code>
Copy after login
Copy after login

回复内容:

<code class="lang-php">$arr = array(
    array(
    'id' => 1,
    'user_name'=>'test1'
    ),
    array(
    'id' => 2,
    'user_name'=>'test2'
    ),
    array(
    'id' => 3,
    'user_name'=>'test3'
    )
);
$arr2 = array(
     array(
    'id' => 1,
    'shop_name'=>'shop1'
    ),
    array(
    'id' => 5,
    'shop_name'=>'shop2'
    ),
    array(
    'id' => 3,
    'shop_name'=>'shop3'
    )
);
怎么根据2个数组id相同的一维数组,将$arr2的shop_name添加到$arr,如果没有相同的id,shop_name为空,形成如下数组:
$good_arr =array(
    array(
    'id' => 1,
    'user_name'=>'test1',
    'shop_name'=>'shop1'
    ),
    array(
    'id' => 2,
    'user_name'=>'test2',
    'shop_name'=>''
    ),
    array(
    'id' => 3,
    'user_name'=>'test3',
    'shop_name'=>'shop3'
    )

);

</code>
Copy after login
Copy after login

我的方案:有更好的吗?O(∩_∩)O谢谢

<code class="lang-php">    /*foreach($arr as $key => &$value) {
        $value['shop_name']='';
    }
        unset( $value );
        */
    foreach($arr as $k2 =>&$v2) {
            $v2['shop_name']='';
        foreach($arr2 as $k =>$v) {
            if($v2['id'] == $v['id']) {
            $v2['shop_name'] = $v['shop_name'];
            }
        }
    }
    print_r($arr);

</code>
Copy after login
Copy after login

我写了个时间复杂度O(n) 比你的时间短
空间复杂度则要多一点

<code><?php $arr = array(
    array(
    'id' => 1,
    'user_name'=>'test1'
    ),
    array(
    'id' => 2,
    'user_name'=>'test2'
    ),
    array(
    'id' => 3,
    'user_name'=>'test3'
    )
);
$arr2 = array(
     array(
    'id' => 1,
    'shop_name'=>'shop1'
    ),
    array(
    'id' => 5,
    'shop_name'=>'shop2'
    ),
    array(
    'id' => 3,
    'shop_name'=>'shop3'
    )
);

$tmp = '';
foreach ($arr2 as $k) { $tmp[$k['id']] = $k; }
foreach ($arr as &$v) {
    $sn = $tmp[$v['id']]['shop_name'];
    $sn = $sn ? $sn :'';
    $v['shop_name'] = $sn;
}

var_dump($arr);

?>
</code>
Copy after login

PHP5.5支持一个很棒的函数array_column()非常适合干这个事情,具体请参考手册:http://cn2.php.net/manual/zh/function.array-column.php。

<code>$shop_name = array_column($arr2, 'shop_name', 'id');
foreach($arr as $key => $item) {
    $exist = array_key_exists($item['id'], $shop_name);
    $arr[$key]['shop_name'] = $exist ? $shop_name[$item['id']] : '';
}
print_r($arr);
</code>
Copy after login

以上代码没有经过测试(因为我本地也没5.5版本的PHP),写法上可能也略丑,但是主要是想介绍一下array_column()这个函数。

如果是用循环做的话,你那样写也是没有问题的,但是我依旧是“能不用嵌套循环就坚决不用嵌套循环”的想法。

<code>$shop_name = array();
foreach($arr2 as $item) $shop_name[$item['id']] = $item['shop_name'];

foreach($arr as $key => $item) {
    $exist = array_key_exists($item['id'], $shop_name);
    $arr[$key]['shop_name'] = $exist ? $shop_name[$item['id']] : '';
}

print_r($arr);
</code>
Copy after login

题外问一句吧,题主你怎么总是在问这种PHP的数组操作问题啊,这种东西查查文档仔细想想就能出来的东西实在是有点....我就不多说什么了。

<code>function filter($item) 
{
    return count($item) == 3 ? $item : false;
}

$newArr = array_filter(array_merge($arr, $arr2), 'filter');
</code>
Copy after login

题主实在不好意思,上面这个回答我弄错了 array_merge 的用法,现在重写一个,已本机测试:

<code>$tempArr = array();
foreach($arr2 as $item) 
    $tempArr[$item['id']] = $item['shop_name'];

foreach($arr as $k => $v) {
    if(array_key_exists($v['id'], $tempArr)) { 
        $arr[$k]['shop_name'] = $tempArr[$v['id']];
    } else {
        $arr[$k]['shop_name'] = '';
    }
}
$newArr = $arr;
</code>
Copy after login

btw,我以前也喜欢写嵌套循环,因为逻辑比较简单,但是自从 @公子 指正后,我已经开始尽量避免这种效率极低的写法,希望答案能帮到题主!:)

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template