将新键添加到多维数组不起作用

时间:2015-12-07 15:49:46

标签: php multidimensional-array

我从频道表

获得多维数组
$sql_channels = get channel sql
$channels = $sql_channels->fetchAll();

我希望在每个频道中添加视频:

foreach($channels as $key=>$channel)
{

$sql_product = get video sql
    $product = $sql_product->fetchAll(PDO::FETCH_ASSOC);

$channels[$key]['videos'] = $product;
}

但只有第一个频道有视频,第二个频道['视频']为空! 像这样出来:

Array
(
    [0] => Array
        (
            [chanel_title] => test0
            [vedios] => Array
                (
                    [0] => Array
                        (
                            [id] => 23 
                        )

                )

        )

    [1] => Array
        (
            [chanel_title] => test1

        )

    [2] => Array
        (
            [chanel_title] => test2

        )

    [3] => Array
        (
            [chanel_title] => test3
        )

    [4] => Array
        (
            [chanel_title] => test4

        )
)

所有频道都包含一个视频。我添加'图像'用这种方法来通道数组和工作正常,但视频不起作用。

1 个答案:

答案 0 :(得分:0)

试试这个:

$sql_channels = get channel sql
$channels = $sql_channels->fetchAll();

$sql_product = get video sql
$product = $sql_product->fetchAll(PDO::FETCH_ASSOC);

// var_dump($product); exit; // uncomment to verify data array

foreach($channels as &$channel_i)
{
    $channel_i['videos'] = $product;
}unset($channel_i);                 

// var_dump($channels); exit; // uncomment to verify output array