How to access the loops for each

时间:2019-01-12 09:17:25

标签: php arrays php-7

I have the array in which I have multiple data that was fetched from database Now I am trying to access the data like that:

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

In this first run it shows the array A. when this loop run second this shows the 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
    )

)

Here is the second array:

    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
    )

)

I want to echo only one item Like If i want to access the comment text then it should be

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

But it's return the undefinded index

Here is the answer after json encode result:

[[{"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"}]]

2 个答案:

答案 0 :(得分:0)

因此,请看一下您的循环,已将$ comments设置为$ comments,尝试:

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

答案 1 :(得分:0)

基于json“结果”,您仅创建了一个包含2个子数组(A和B)的父数组。要访问第二个孩子,请在循环中引用val patchModule = listOf( "--patch-module", "commons.logging=" + spec2File["org.slf4j:jcl-over-slf4j"].toString(), "--patch-module", "org.apache.commons.logging=" + spec2File["org.slf4j:jcl-over-slf4j"].toString() ) patchModule.forEach({it -> println(it)}) tasks { withType<JavaCompile> { doFirst { options.compilerArgs.addAll(listOf( "--release", "11", "--module-path", classpath.asPath ) + patchModule) // println("Args for for ${name} are ${options.allCompilerArgs}") } } }

代码:(Demo

[1]

输出:

$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";
}

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

相关问题