PHP:使用公共密钥合并多个数组

时间:2017-08-04 15:55:09

标签: php multidimensional-array

我的脚本中有5个不同的数组:

$array1 = array(

array( "id"=>"1", "title"=>"Batman" ),
array( "id"=>"2", "title"=>"Spiderman" ),
array( "id"=>"3", "title"=>"Titanic" ),
array( "id"=>"4", "title"=>"Dracula" ),

);

$array2 = array(

array( "id"=>"1", "releasedate"=>"1926" ),
array( "id"=>"2", "releasedate"=>"1956" ),
array( "id"=>"3", "releasedate"=>"2001" ),
array( "id"=>"4", "releasedate"=>"1982" ),

);
等等...... 如您所见,有关电影编号1的信息在所有阵列上被分割(事实上 - > 5个阵列)。然后我想合并我的所有数组来得到这样的东西:

$array_FINAL = array(

array( "id"=>"1", "title"=>"Batman", "releasedate"=>"1926" ),
array( "id"=>"2", "title"=>"Spiderman", "releasedate"=>"1956" ),
array( "id"=>"3", "title"=>"Titanic", "releasedate"=>"2001" ),
array( "id"=>"4", "title"=>"Dracula", "releasedate"=>"1982" ),

);

我试过array_merge,array_combine,没有好结果。我还检查了stackoverflow上的其他主题,但没有人帮助我(我可能会错过我需要的那个!)

有任何帮助吗? :)

编辑:抱歉,我确实会提供更多详细信息......数组中的行可能是misc。顺序,然后根据我的代码示例:电影"蝙蝠侠"可以在第一个数组的第一行,但在第二个数组的第三行......

4 个答案:

答案 0 :(得分:1)

其中一个解决方案是:

$array1 = array(

array( "id"=>"1", "title"=>"Batman" ),
array( "id"=>"2", "title"=>"Spiderman" ),
array( "id"=>"3", "title"=>"Titanic" ),
array( "id"=>"4", "title"=>"Dracula" ),

);

$array2 = array(

array( "id"=>"1", "releasedate"=>"1926" ),
array( "id"=>"2", "releasedate"=>"1956" ),
array( "id"=>"3", "releasedate"=>"2001" ),
array( "id"=>"4", "releasedate"=>"1982" ),

);

// here we create pairs `id => releasedate`    
$new_array2 = [];
foreach ($array2 as $v) {
    $new_array2[$v['id']] = $v['releasedate'];
}

foreach ($array1 as &$value) {
    // here we try to find key `$value['id']` in `$new_array2`
    // and get it's value
    if (isset($new_array2[$value['id']])) {
        $value['releasedate'] = $new_array2[$value['id']];
    }
}

如果您100%确定两个数组中id的订单相同,您可以:

$i = 0;
foreach ($array1 as &$value) {
    $value['releasedate'] = $array2[$i]['releasedate'];
    $i++;
}

答案 1 :(得分:1)

如果两个数组的顺序相同,您只需使用

即可
$array_final = array_replace_recursive($array1, $array2);

但是,如果要通过“id”合并它们,则需要循环它们。复杂度为O(m * n)的解决方案:

$array_final = array();
// loop through all movies in array1
foreach($array1 as $movie){
    foreach($array2 as $movie_release) {
        if ($movie['id'] == $movie_release['id']) {
            $movie['releasedate'] = $movie_release['releasedate'];
            break;
        }
    }
    $array_final[] = $movie;
}

稍微复杂的O(m + n):

// create arrays with the ID as key
$titles = array_column($array1, 'title', 'id');
$releasedates = array_column($array2, 'releasedate', 'id');

$array_final = array();

foreach($titles as $id => $title) {
    $array_final[] = array(
        'id' => $id,
        'title' => $title,
        'releasedate' => $releasedates[$id]
    );
}

答案 2 :(得分:0)

请参阅此代码原型(这很简单,我不需要解释):

$fArr = [];
for ( $i = 1; $i <= 4; $i++ ) {
    $fArr[] = [
        'id' => $i,
        'title' => $array1[$i - 1]['title'],
        'releasedate' => $array2[$i - 1]['releasedate'],
    ];
}

Sandbox code example

答案 3 :(得分:0)

假设您的所有五个数组都包含id键,您可以使用嵌套的foreach循环执行此操作。

foreach (array_merge($array1, $array2, $array3, $array4, $array5) as $item) {
    foreach ($item as $key => $value) {
        $result[$item['id']][$key] = $value;
    }
}

外部foreach迭代所有数组中的所有行。内部循环遍历每一列,并将其值分配给结果数组中的相应键。