简体   繁体   English

如何在数组php中循环遍历json数组

[英]How to loop through json array within an array php

I need help traversing an array within array, I only need to loop through certain arrays, for example, how would I just lop through the names array? 我需要帮助遍历数组中的数组,我只需要遍历某些数组,例如,我如何只是通过名称数组?

Array
(
    [@total_records] => 10
    [@total_matching_records] => 10
    [@available_records] => 200
    [@available_matching_records] => 12
    [query] => Array
        (
            [summary] => Array
                (
                    [emails] => Array
                        (
                            [0] => Array
                                (
                                    [content] => jonathan.lyon@gmail.com
                                )

                        )

                )

        )

    [results] => Array
        (
            [person] => Array
                (
                    [@match_score] => 1
                    [summary] => Array
                        (
                            [names] => Array
                                (
                                    [0] => Array
                                        (
                                            [first] => Jonathan
                                            [last] => Lyon
                                            [display] => Jonathan Lyon
                                        )
                                    [1] => Array
                                        (
                                            [first] => Jonathan
                                            [last] => Jordan
                                            [display] => Jonathan Jordan
                                        )
                                )

I have tried this but can't get it to work:- 我试过这个但是无法让它起作用: -

foreach($json_output['results']['person']['summary']['names'] as $key => $val) {
echo $key.": ".$val."</br>";
}

Any help would be greatly appreciated. 任何帮助将不胜感激。

Thanks 谢谢

Jonathan 乔纳森

Have you tried this 你试过这个吗?

foreach($json_output['results']['person']['summary']['names'] as $key => $val) {
    echo $key.": ".$val['display']."</br>";
}

?

In your example you trying to echo $key . 在您的示例中,您尝试echo $key Key in your case $key is array index (integer). 键入你的情况$key是数组索引(整数)。 Are you sure you realy need that? 你确定你真的需要吗?

You nedd to change your code to: 您需要将代码更改为:

foreach($json_output['results']['person']['summary']['names'] as $val) {
    echo $val['display']."</br>";
}

Are you getting any error output? 你有任何错误输出? That would help a lot. 这会有很大帮助。 I can see also that $val in this case is an array so you don't want to echo that. 我还可以看到在这种情况下$ val是一个数组,所以你不想回应它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM