0

我需要帮助遍历数组中的数组,我只需要遍历某些数组,例如,我将如何遍历名称数组?

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
                                        )
                                )

我已经尝试过了,但无法让它工作:-

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

任何帮助将不胜感激。

谢谢

乔纳森

4

3 回答 3

1

你试过这个

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

?

于 2012-07-11T07:17:16.330 回答
1

在您的示例中,您尝试echo $key. 您的情况的关键$key是数组索引(整数)。你确定你真的需要那个吗?

您需要将代码更改为:

foreach($json_output['results']['person']['summary']['names'] as $val) {
    echo $val['display']."</br>";
}
于 2012-07-11T07:19:22.947 回答
0

您是否收到任何错误输出?那会有很大帮助。我还可以看到在这种情况下 $val 是一个数组,所以你不想回应它。

于 2012-07-11T07:16:19.893 回答