簡體   English   中英

如何訪問 Object 中具有數組的 JSON 的值?

[英]How can I access the value of JSON which has Array in Object?

我想知道一種簡單的方法來訪問 JSON 之后的值,它擁有一個數組。 我在 PHP 編碼:

<?php

$json = '{"flight": "7800", "city": "New York", "time":[{"Houston":"17:34","Los Angles":"21:23"}]}';   

$str = json_decode($json);
echo $str->city;   // New York
//echo $str->time->Houston;    // How can I access the valus of Houston:  17:34  

?>

將 Json-String 解碼為關聯數組:

$json = '{"flight": "7800", "city": "New York", "time":[{"Houston":"17:34","Los Angeles":"21:23"}]}';
   
$flight = json_decode($json, true);// You get associative array in this case

echo "<br>" . $flight['city'];   // New York
echo "<br>" . $flight['time'][0]['Houston'];
echo "<br>" . $flight['time'][0]['Los Angeles'];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM