首页 > 解决方案 > 在 PHP 中访问 JSON 数据中的值时遇到问题

问题描述

我必须尝试访问 PHP 中 JSON 数据中 4.9 的“文本”值。

$json = '{"destination_addresses" : [ "Ibadan-Abeokuta Rd, Mokola, Ibadan, Nigeria" ],
 "origin_addresses" : [ "Ogunwale Crescent, Ibadan, Nigeria" ], 
 "rows" : [ 
     { "elements" : [
          { "distance" : 
            { "text" : "4.9 mi", "value" : 7819 }, 
            "duration" : { "text" : "18 mins", "value" : 1067 }, 
            "status" : "OK" }
                
                    ]
         } 
        ]
         
         , "status" : "OK" }';

标签: phpjson

解决方案


您的 JSON 有点复杂。text我在嵌套 JSON 的访问属性下面有一个解决方案。

$array = json_decode( $json, true ); 
echo ($array['rows'][0] ["elements"][0]['distance']['text']);

推荐阅读