【问题标题】:PHP ?Nested ArrayPHP ? 嵌套数组
【发布时间】:2022-01-18 14:23:25
【问题描述】:

我有一个从 Google 地图返回的数组,如下所示:

array(4) { 
    ["destination_addresses"]=> array(4) { 
        [0]=> string(19) "Walsall WS2 9PS, UK" 
        [1]=> string(19) "Walsall WS2 9PS, UK" 
        [2]=> string(19) "Walsall WS2 9PS, UK" 
        [3]=> string(26) "Wolverhampton WV10 0QP, UK" 
        } 
    ["origin_addresses"]=> array(1) { 
        [0]=> string(18) "Stone ST15 0FL, UK" 
        }   
    ["rows"]=> array(1) { 
        [0]=> array(1) { 
            ["elements"]=> array(4) { 
                [0]=> array(3) { 
                    ["distance"]=> array(2) { 
                        ["text"]=> string(7) "41.9 km" 
                        ["value"]=> int(41947) 
                    } 
                    ["duration"]=> array(2) { 
                        ["text"]=> string(7) "36 mins" 
                        ["value"]=> int(2134) 
                    } 
                    ["status"]=> string(2) "OK" 
                } 
                [1]=> array(3) { 
                    ["distance"]=> array(2) { 
                        ["text"]=> string(7) "41.9 km" 
                        ["value"]=> int(41947) 
                    } 
                    ["duration"]=> array(2) { 
                        ["text"]=> string(7) "36 mins" 
                        ["value"]=> int(2134) 
                    }
                    ["status"]=> string(2) "OK"
                 } 
                [2]=> array(3) { 
                    ["distance"]=> array(2) { 
                        ["text"]=> string(7) "41.9 km" 
                        ["value"]=> int(41947)
                    } 
                    ["duration"]=> array(2) { 
                        ["text"]=> string(7) "36 mins" 
                        ["value"]=> int(2134) 
                    } 
                    ["status"]=> string(2) "OK"
                } 
                [3]=> array(3) { 
                    ["distance"]=> array(2) { 
                        ["text"]=> string(7) "40.9 km" 
                        ["value"]=> int(40924) 
                    } 
                    ["duration"]=> array(2) { 
                        ["text"]=> string(7) "41 mins" 
                        ["value"]=> int(2458) 
                    } 
                    ["status"]=> string(2) "OK" 
                } 
            } 
        } 
    } 
    ["status"]=> string(2) "OK" 
}

我希望能够遍历数组,并打印目标地址以及距离和时间值。

这打印目标地址ok:

    $length=count($properties);
    for($a=0;$a<$length;$a++)
    {
        echo '</br>Index is ' . $a . ' | id is ' . $index[$a] . ' | destination is ' . $properties["destination_addresses"][$a] . ' | time is ';

    }

但我不知道如何打印其余部分。我整晚都在用头撞墙!

谢谢你的期待。

【问题讨论】:

  • 请分享var_export($the-source-array),它可以让志愿者轻松复制/帮助你。谢谢
  • $properties[duration][text]?大概。如果您使用结构化数据而不是单行数据,则可读性会更好。
  • 第二次查看后应该是$properties['rows'][$a]['elements'][0]['duration']['text']; // 36 mins
  • @pavel 迫不及待地想看看第三个视图可能会给出什么:)
  • 如果根据我对您的数组结构的假设我没有错,它应该是$properties['rows'][0]['elements'][$a]['duration']['text'];

标签: php multidimensional-array


【解决方案1】:

要遍历所有这些旅程并打印每个潜在旅程的距离和时间,请创建一个 foreach 循环,然后使用 $idx 来处理 muleiple 数组的从属部分

foreach( $destination['destination_addresses'] as $idx => $to ) {
    if ( $destination['rows'][0]['elements'][$idx]['status'] == 'OK' ) {
        echo 'Going from ' . $destination['origin_addresses'][0];
        echo ' To ' . $to;
        echo ' Distance ' . $destination['rows'][0]['elements'][$idx]['distance']['text'];
        echo ' Duration ' . $destination['rows'][0]['elements'][$idx]['duration']['text'];
    } else {
        //whatever you do if the status is not OK
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-17
    • 2018-07-04
    • 2011-04-10
    • 2012-08-07
    • 2020-08-24
    • 1970-01-01
    相关资源
    最近更新 更多