如何将一个多维数组中的特定值插入另一个?

时间:2017-03-08 09:43:23

标签: php arrays multidimensional-array

数组A:

Array
(
    [0] => Array
        (
            [id] => 1
            [0] => 1
            [project_id] => a
            [1] => a
            [title] => Yellow
            [2] => Yellow
        )

    [1] => Array
        (
            [id] => 2
            [0] => 2
            [project_id] => b
            [1] => b
            [title] => blue
            [2] => blue
        )

    [2] => Array
        (
            [id] => 3
            [0] => 3
            [project_id] => c
            [1] => c
            [title] => green
            [2] => green
        )

)

数组B:

Array
(
    [0] => Array
        (
            [id] => 1
            [0] => 1
            [project_id] => b
            [1] => b
            [name] => Ross
            [4] => Ross
            [15] => 
            [category] => horse
            [16] => horse
        )

    [1] => Array
        (
            [id] => 2
            [0] => 2
            [project_id] => b
            [1] => b
            [name] => Capone
            [4] => Capone
            [category] => cat
            [16] => cat
        )

    [2] => Array
        (
            [id] => 3
            [0] => 3
            [project_id] => c
            [1] => c
            [name] => Streisand
            [4] => Streisand
            [category] => elephant
            [16] => elephant
        )

    [3] => Array
        (
            [id] => 4
            [0] => 4
            [project_id] => a
            [1] => a
            [name] => Harper
            [4] => Harper
            [category] => frog
            [16] => frog
        )

)

我想根据project_id将Array A中的值插入Array B

我想要达到的结果是:

数组C:

Array
(
    [0] => Array
        (
            [id] => 1
            [project_id] => a
            [title] => Yellow
        )

    [1] => Array
        (
            [id] => 2
            [project_id] => b
            [title] => blue
            [horse] => Ross
            [cat] => Capone
        )

    [2] => Array
        (
            [id] => 3
            [project_id] => c
            [title] => green
            [elephant] => Streisand
            [frog] => Harper
        )

)

我真的迷路了,我测试过使用array_merge,array_column和array_push。但我找不到解决这个问题的正确方法。也许我不想这样做?我很高兴任何提示!

2 个答案:

答案 0 :(得分:1)

希望这是你想要的:

GIT_SSH_COMMAND

演示:

https://eval.in/750450

答案 1 :(得分:1)

只需使用两个foreach

<?php

$a = [
        [
            'id' => 1,
            '0' => 1,
            'project_id' => 'a',
            '1' => 'a',
            'title' => 'Yellow',
            '2' => 'Yellow'
        ],
        [
            'id' => 2,
            '0' => 2,
            'project_id' => 'b',
            '1' => 'b',
            'title' => 'blue',
            '2' => 'blue'
        ],
        [
            'id' => 3,
            '0' => 3,
            'project_id' => 'c',
            '1' => 'c',
            'title' => 'green',
            '2' => 'green'
        ]
];

$b = [
    [
            'id' => 1,
            '0' => 1,
            'project_id' => 'b',
            '1' => 'b',
            'name' => 'Ross',
            '4' => 'Ross',
            'category' => 'horse',
            '16' => 'horse'
    ],
    [
            'id' => 2,
            '0' => 2,
            'project_id' => 'b',
            '1' => 'b',
            'name' => 'Capone',
            '4' => 'Capone',
            'category' => 'cat',
            '16' => 'cat'
    ]
];

$final_array = [];

foreach($a as $item) {
    foreach($b as $item2) {
        if ($item['project_id'] == $item2['project_id']){
            $object = array_merge($item,$item2);
        } else {
            $object = $item;
        }
    }
    $final_array[] = $object;
}

var_dump($final_array);