我有一個JSON字符串,我想解碼此字符串讓PHP數組並把它添加到數據庫中,但是當我嘗試調用數組的某些部分我沒有得到任何東西:JSON解碼與PHP PDO
<?php
$json = '{"zoom":13,"tilt":0,"mapTypeId":"hybrid","center":{"lat":45.38591280296377,"lng":19.936323169799834},"overlays":[{"type":"polygon","title":"Polje 1","content":"Vojvodina","fillColor":"#ffbf1a","fillOpacity":0.3,"strokeColor":"#000","strokeOpacity":0.8,"strokeWeight":3,"paths":[[{"lat":"45.37867863632308","lng":"19.948768615722656"},{"lat":"45.370719925928746","lng":"19.941558837890625"},{"lat":"45.36227764550136","lng":"19.92816925048828"},{"lat":"45.359262240003495","lng":"19.942245483398438"},{"lat":"45.35588479505299","lng":"19.955806732177734"},{"lat":"45.35974471568275","lng":"19.958553314208984"},{"lat":"45.36312193024184","lng":"19.959583282470703"},{"lat":"45.365534102931655","lng":"19.960613250732422"},{"lat":"45.36529289029106","lng":"19.96490478515625"},{"lat":"45.37084052080666","lng":"19.970226287841797"}]]}]}';
$arr = (json_decode($json, true));
echo $arr['paths'];
?>
那我爲什麼不能打印$arr['paths']
???
嘗試print_r($ arr);知道數組的結構來正確獲取'paths' –
因爲'$ arr ['paths']'不存在。我假設你實際上是在$ arr ['overlays'] [0] ['paths']'之後。此外,'$ arr ['overlays'] [0] ['paths']'是一個數組,因此試圖回顯它將不起作用,查看它的結構,您可以執行print_r($ arr ['overlays'] [0] ['paths'])' – MLeFevre