如何访问每个循环

How to access the loops for each

我有一个数组,其中有多个从数据库中获取的数据现在我正在尝试访问这样的数据:

<?php foreach ($comments as $comments) {
 print_r($comments);
}

在第一个 运行 中显示数组 A。当此循环 运行 第二个中显示 B

(A)
Array
(
    [0] => Array
        (
            [u] => ali
            [user_date] => 2011-01-19
            [up] => superthumb
        )
[1] => Array
    (
        [u] => Lucky
        [user_date] => 2011-01-19
        [up] => superthumb
    )

[2] => Array
    (
        [u] => Lucky
        [user_date] => 2011-01-19
        [up] => superthumb
    )

[3] => Array
    (
        [u] => aq
        [user_date] => 2011-01-19
        [up] => cute_glassess_girl
    )

[4] => Array
    (
        [u] => aq
        [user_date] => 2011-01-19
        [up] => cute_glassess_girl
    )

[5] => Array
    (
        [u] => aq
        [user_date] => 2011-01-19
        [up] => cute_glassess_girl
    )

[6] => Array
    (
        [u] => aq
        [user_date] => 2011-01-19
        [up] => cute_glassess_girl
    )

)

这是第二个数组:

    Array
(
    [0] => Array
        (
            [comment_id] => 7
            [comment_text] => kHi there i am your great fan
            [commenter_id] => 46
            [commented_post_fk_id] => 2
            [comment_date] => 2011-01-19
            [deleted] => 0
        )
[1] => Array
    (
        [comment_id] => 8
        [comment_text] => kHi there i am your great fan
        [commenter_id] => 47
        [commented_post_fk_id] => 2
        [comment_date] => 2011-01-19
        [deleted] => 0
    )

[2] => Array
    (
        [comment_id] => 9
        [comment_text] => lucky0473649
        [commenter_id] => 48
        [commented_post_fk_id] => 2
        [comment_date] => 2011-01-19
        [deleted] => 0
    )

[3] => Array
    (
        [comment_id] => 10
        [comment_text] => lucky0473649
        [commenter_id] => 49
        [commented_post_fk_id] => 2
        [comment_date] => 2011-01-19
        [deleted] => 0
    )

[4] => Array
    (
        [comment_id] => 11
        [comment_text] => lucky0473649
        [commenter_id] => 50
        [commented_post_fk_id] => 2
        [comment_date] => 2011-01-19
        [deleted] => 0
    )

[5] => Array
    (
        [comment_id] => 12
        [comment_text] => lucky0473649
        [commenter_id] => 51
        [commented_post_fk_id] => 2
        [comment_date] => 2011-01-19
        [deleted] => 0
    )

[6] => Array
    (
        [comment_id] => 17
        [comment_text] => ali
        [commenter_id] => 28
        [commented_post_fk_id] => 2
        [comment_date] => 2011-01-19
        [deleted] => 0
    )

[7] => Array
    (
        [comment_id] => 18
        [comment_text] => ali
        [commenter_id] => 28
        [commented_post_fk_id] => 2
        [comment_date] => 2011-01-19
        [deleted] => 0
    )

)

我只想回显一个项目 如果我想访问评论文本,那么它应该是

foreach ($comments as $comments) {
 echo $comments['comment_text'];
}

但它是return未定义的索引

这里是json编码后的答案:

[[{"u":"ali","user_date":"2011-01-19","up":"superthumb"},{"u":"Lucky","user_date":"2011-01-19","up":"superthumb"},{"u":"Lucky","user_date":"2011-01-19","up":"superthumb"},{"u":"aq","user_date":"2011-01-19","up":"cute_glassess_girl"},{"u":"aq","user_date":"2011-01-19","up":"cute_glassess_girl"},{"u":"aq","user_date":"2011-01-19","up":"cute_glassess_girl"},{"u":"aq","user_date":"2011-01-19","up":"cute_glassess_girl"}],[{"comment_id":"7","comment_text":"kHi there i am your great fan","commenter_id":"46","commented_post_fk_id":"2","comment_date":"2011-01-19","deleted":"0"},{"comment_id":"8","comment_text":"kHi there i am your great fan","commenter_id":"47","commented_post_fk_id":"2","comment_date":"2011-01-19","deleted":"0"},{"comment_id":"9","comment_text":"lucky0473649","commenter_id":"48","commented_post_fk_id":"2","comment_date":"2011-01-19","deleted":"0"},{"comment_id":"10","comment_text":"lucky0473649","commenter_id":"49","commented_post_fk_id":"2","comment_date":"2011-01-19","deleted":"0"},{"comment_id":"11","comment_text":"lucky0473649","commenter_id":"50","commented_post_fk_id":"2","comment_date":"2011-01-19","deleted":"0"},{"comment_id":"12","comment_text":"lucky0473649","commenter_id":"51","commented_post_fk_id":"2","comment_date":"2011-01-19","deleted":"0"},{"comment_id":"17","comment_text":"ali","commenter_id":"28","commented_post_fk_id":"2","comment_date":"2011-01-19","deleted":"0"},{"comment_id":"18","comment_text":"ali","commenter_id":"28","commented_post_fk_id":"2","comment_date":"2011-01-19","deleted":"0"}]]

所以,看看你的循环,你已经将 $comments 设置为 $comments,试试看:

foreach ($comments as $comment) {
 echo $comment['comment_text'];
}

根据您的 json "result",您仅创建了一个包含 2 个 child 数组(A 和 B)的 parent 数组。要访问第二个 child,请在循环中引用 [1]

代码:(Demo)

$json = '[[{"u":"ali","user_date":"2011-01-19","up":"superthumb"},{"u":"Lucky","user_date":"2011-01-19","up":"superthumb"},{"u":"Lucky","user_date":"2011-01-19","up":"superthumb"},{"u":"aq","user_date":"2011-01-19","up":"cute_glassess_girl"},{"u":"aq","user_date":"2011-01-19","up":"cute_glassess_girl"},{"u":"aq","user_date":"2011-01-19","up":"cute_glassess_girl"},{"u":"aq","user_date":"2011-01-19","up":"cute_glassess_girl"}],[{"comment_id":"7","comment_text":"kHi there i am your great fan","commenter_id":"46","commented_post_fk_id":"2","comment_date":"2011-01-19","deleted":"0"},{"comment_id":"8","comment_text":"kHi there i am your great fan","commenter_id":"47","commented_post_fk_id":"2","comment_date":"2011-01-19","deleted":"0"},{"comment_id":"9","comment_text":"lucky0473649","commenter_id":"48","commented_post_fk_id":"2","comment_date":"2011-01-19","deleted":"0"},{"comment_id":"10","comment_text":"lucky0473649","commenter_id":"49","commented_post_fk_id":"2","comment_date":"2011-01-19","deleted":"0"},{"comment_id":"11","comment_text":"lucky0473649","commenter_id":"50","commented_post_fk_id":"2","comment_date":"2011-01-19","deleted":"0"},{"comment_id":"12","comment_text":"lucky0473649","commenter_id":"51","commented_post_fk_id":"2","comment_date":"2011-01-19","deleted":"0"},{"comment_id":"17","comment_text":"ali","commenter_id":"28","commented_post_fk_id":"2","comment_date":"2011-01-19","deleted":"0"},{"comment_id":"18","comment_text":"ali","commenter_id":"28","commented_post_fk_id":"2","comment_date":"2011-01-19","deleted":"0"}]]';

$array = json_decode($json, true);
// var_export($array);

foreach ($array[1] as $comment) {
    echo $comment["comment_text"] , "\n";
}

输出:

kHi there i am your great fan
kHi there i am your great fan
lucky0473649
lucky0473649
lucky0473649
lucky0473649
ali
ali

这解决了您的问题,但老实说,我看不出在循环之前以更复杂的结构组合数组的意义。