0

我想将文件名合并到同一个键中。下面是两个数组,我尝试使用 array_merge 但它没有合并到同一个键中

Array
(
    [0] => Array
        (
            [episode_id] => 2
            [episode_number] => 1
            [series] => 2012
        )

    [1] => Array
        (
            [episode_id] => 2
            [episode_number] => 2
            [series] => 2013
        )

)
Array
(
    [0] => Array
        (
            [filename] => Enq9nNGnMFY
        )

    [1] => Array
        (
            [filename] => Enq9nsdfdsNGnMFY
        )

)

预期的输出应该是

Array
(
    [0] => Array
        (
            [episode_id] => 2
            [episode_number] => 1
            [series] => 2012
            [filename] => Enq9nNGnMFY
        )

    [1] => Array
        (
            [episode_id] => 2
            [episode_number] => 2
            [series] => 2013
            [filename] => Enq9nsdfdsNGnMFY
        )

)

有人能帮忙吗 !!!!!!!!!!

4

3 回答 3

1
foreach ($first_array as $key => $item){
$second_array[$key] = array_merge($second_array[$key],$item);
}

第二个数组将包含您想要的内容。

于 2013-10-16T02:58:45.470 回答
0

试试这个:

foreach($array_1 as $key => $value){
    foreach($array_2[$key] as $key2 => $value2){
        array_push($array_1[$key], $value2);
    }
}

$array_1 应该是你的组合数组,即使你有的不仅仅是文件名。

于 2013-10-16T02:57:15.740 回答
0

试试这个:

<?php
        $array_1 = Array
            (Array('episode_id' => 2, 'episode_number' => 1, 'series' => 2012),
            Array('episode_id' => 2, 'episode_number' => 2, 'series' => 2013)
        );

        $array_2 = Array(Array('filename' => 'Enq9nNGnMFY'), Array('filename' => 'Enq9nsdfdsNGnMFY'));

        foreach ($array_1 as $key => $value) {
            foreach ($array_2[$key] as $key2 => $value2) {
                $array_1[$key][$key2] = $value2;
            }
        }



        echo '<pre>';
        print_r($array_1);
        ?>

第一个数组是你想要的结果

于 2013-10-16T07:23:10.013 回答