加入两个多维数组

时间:2018-05-26 20:22:36

标签: php arrays

我有两个数组,每个数组来自不同的数据库查询,我不能在数据库中加入它们所以我必须使用php加入它们。 第一个:

Array
(
    [0] => Array
        (
            [id] => 23
            [host_id] => 5
            [pos_id] => 2
            [status] => 1
        )

    [1] => Array
        (
            [id] => 25
            [host_id] => 5
            [pos_id] => 1
            [status] => 1
        )

    [2] => Array
        (
            [id] => 24
            [host_id] => 5
            [pos_id] => 2
            [status] => 1
        )

)

我想在pos_idid

上加入此数组
Array
(
    [0] => Array
        (
            [id] => 1
            [name] => Front
        )

    [1] => Array
        (
            [id] => 2
            [name] => Back
        )
)

这样,结果就是:

Array
(
    [0] => Array
        (
            [id] => 23
            [host_id] => 5
            [pos_id] => 2
            [name] => Back
            [status] => 1
        )

    [1] => Array
        (
            [id] => 25
            [host_id] => 5
            [pos_id] => 1
            [name] => Front

            [status] => 1
        )

    [2] => Array
        (
            [id] => 24
            [host_id] => 5
            [pos_id] => 2
            [name] => Back
            [status] => 1
        )

)

我使用的代码使用内部循环,匹配值并推送到数组:

foreach($events as &$event) {
    foreach($positions as $position) {
        if($player[pos_id] == $position[id]){
            array_push($event,"name",$position[name]);
        }
    }
}

0 个答案:

没有答案