合并2个不同的数组并在php中形成一个数组

时间:2015-04-15 11:36:06

标签: php array-merge

我需要合并两个不同的数组并形成一个数组。关键字段在两个数组中都具有相同的值。

$array1=Array ( 
[0] => Array ( [key] => 39 [url] => www.youtube.com/embed/23sdg234 ) 
[1] => Array ( [key] => 39 [url] => player.vimeo.com/video/s3423sfsdf ) 
[2] => Array ( [key] => 39 [url] => www.youtube.com/embed/wersd2323 )
[3] => Array ( [key] => 40 [url] => www.youtube.com/embed/3423sfsd )
[4] => Array ( [key] => 40 [url] => www.youtube.com/embed/asd23423 )
) 

array1需要根据key = key1

从array2获取值[firstName],[lastName]
$array2=Array ( 
[0] => Array ( [key1] => 39 [firstName] => xyz [lastName] => s )
[1] => Array ( [key1] => 40 [firstName] => abc [lastName] => e )
) 

结果应该形成如下数组。

$result=Array ( 
[0] => Array ( [key] => 39  [firstName] => xyz [lastName] => s [url] => www.youtube.com/embed/23sdg234 ) 
[1] => Array ( [key] => 39  [firstName] => xyz [lastName] => s [url] => player.vimeo.com/video/s3423sfsdf ) 
[2] => Array ( [key] => 39  [firstName] => xyz [lastName] => s [url] => www.youtube.com/embed/wersd2323 )
[3] => Array ( [key] => 40  [firstName] => abc [lastName] => e [url] => www.youtube.com/embed/3423sfsd )
[4] => Array ( [key] => 40  [firstName] => abc [lastName] => e [url] => www.youtube.com/embed/asd23423 )
) 

提前非常感谢你。

4 个答案:

答案 0 :(得分:0)

<?php
foreach($array1 as $vid)
{
    $found = false;
    foreach ($array2 as $user)
        {
            if ($vid['key'] === $user['key1'])
            {
                $found = true;
                break 1;
            }
        }
        if ($found)
        {
            $result[] = array(
                               'key' => $vid['key'] , 
                               'firstname' => $user['firstname'] , 
                               'lastname' => $user['lastname'] , 
                               'url' => $array1['url']
                             );
        }
        else
        {
            $result[] = array(
                               'key' => $vid['key'] , 
                               'firstname' => "Unknown" , 
                               'lastname' => "Unknown" , 
                               'url' => $array1['url']
                             );
        }
}

没有对它进行测试,但应该做好。 它基本上通过Array1中的每个项循环。然后对于每个项目,它检查Array2中是否存在相同的密钥。然后,如果密钥匹配,它将所有数据写入新数组。如果未找到匹配的密钥,则“名称”字段将填充“未知”。这只是解决了基本问题,您可能希望根据自己的需要进行调整。

答案 1 :(得分:0)

天真的方式:

foreach ($array1 as &$values) {
    foreach ($array2 as $mixin) {
        if ($mixin['key'] == $values['key']) {
            $values += $mixin;
        }
    }
}

花哨,更优化的方式:

$mixins = array_combine(array_column($array2, 'key'), $array2);
$result = array_map(
    function (array $values) use ($mixins) { return $values += $mixins[$values['key']]; },
    $array1
);

答案 2 :(得分:0)

$array1=Array ( 
0 => Array ( 'key' => 39, 'url' => "www.youtube.com/embed/23sdg234" ),
1 => Array ( 'key' => 39, 'url' => "player.vimeo.com/video/s3423sfsdf" ), 
2 => Array ( 'key' => 39, 'url' => "www.youtube.com/embed/wersd2323" ),
3 => Array ( 'key' => 40, 'url' => "www.youtube.com/embed/3423sfsd" ),
4 => Array ( 'key' => 40, 'url' => "www.youtube.com/embed/asd23423" ),
);
$array2=Array ( 
0 => Array ( 'key1' => 39, 'firstName' => 'xyz', 'lastName' => 's' ),
1 => Array ( 'key1' => 40, 'firstName' => 'abc', 'lastName' => 'e' ),
);
$result=array();

foreach($array1 as $da){
    $fName='';
    $lName='';
    foreach($array2 as $da2){
        if($da['key']==$da2['key1']){
            $fName=$da2['firstName'];
            $lName=$da2['lastName'];
            break;
        }
    }
    $result[]=array(
        'key'=>$da['key'],
        'firstName'=>$fName,
        'lastName'=>$lName,
        'url'=>$da['url'],
    );
}
print_r($result);

答案 3 :(得分:-1)

浏览第一个数组,并创建一个组合:

$combinedArray = [];

array_walk(function($val,$key) use ($array1,$combinedArray){
    if(array_key_exists($key,$array1))
    {
        $combinedArray[$key] = array_merge($val,$array1[$key]);
    }
    else
    {
        $combinedArray[$key] = $val;
    }
},$array);