为什么在foreach循环中说未定义的偏移2

时间:2016-09-25 10:05:50

标签: php arrays

$snippet变量具有以下值:

Array
(
    [0] => Array
    (
        [0] => Array
            (
                [id] => 1
                [user_id] => 2
                [title] => Title
                [detail] => null
                [lang_id] => 19
                [edited] => false
                [last_edited] => 
                [created_date] => 2016-09-25 02:40:03
            )

        [1] => Array
            (
                [id] => 2
                [user_id] => 2
                [title] => Some other title
                [detail] => null
                [lang_id] => 19
                [edited] => false
                [last_edited] => 
                [created_date] => 2016-09-25 02:40:03
            )

        [2] => Array
            (
                [id] => 3
                [user_id] => 2
                [title] => Huh!
                [detail] => ...
                [lang_id] => 19
                [edited] => false
                [last_edited] => 
                [created_date] => 2016-09-25 03:04:48
            )

    )

    [1] => Array
    (
        [0] => Array
            (
                [id] => 1
                [user_id] => 2
                [title] => Title
                [detail] => null
                [lang_id] => 19
                [edited] => false
                [last_edited] => 
                [created_date] => 2016-09-25 02:40:03
            )

        [1] => Array
            (
                [id] => 2
                [user_id] => 2
                [title] => Some other title
                [detail] => null
                [lang_id] => 19
                [edited] => false
                [last_edited] => 
                [created_date] => 2016-09-25 02:40:03
            )

        [2] => Array
            (
                [id] => 3
                [user_id] => 2
                [title] => Huh!
                [detail] => ...
                [lang_id] => 19
                [edited] => false
                [last_edited] => 
                [created_date] => 2016-09-25 03:04:48
            )

    )

    [2] => Array
    (
        [0] => Array
            (
                [id] => 4
                [user_id] => 2
                [title] => apache
                [detail] => ...
                [lang_id] => 1
                [edited] => false
                [last_edited] => 
                [created_date] => 2016-09-25 03:05:06
            )

    )

)

我希望我的结果如下:

Title
Some other title
Huh!
Title
Some other title
Huh!
apache

所以我尝试了以下内容:

<?php foreach ($snippet as $key => $value): ?>
    <?= $value[$key]['title'] ?>
    <br />
<?php endforeach ?>

但结果是:

Title
Some other title

Notice: Undefined offset: 2

我无法弄清楚我做错了什么。

2 个答案:

答案 0 :(得分:5)

试试这个:

<?php 
     foreach ($snippet as $value):
        foreach($value as $title):
           echo $title['title']."<br>";
        endforeach;
      endforeach; 
 ?>

答案 1 :(得分:1)

使用(.keys js/Object obj),您可以在array_column()的帮助下打印您的标题。这是基于您的尝试,因为您尝试使用一个implode()

foreach()

试试这个,让我知道它是否有效。