2014-03-06 81 views
0

我有一個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'] ???

+0

嘗試print_r($ arr);知道數組的結構來正確獲取'paths' –

+0

因爲'$ arr ['paths']'不存在。我假設你實際上是在$ arr ['overlays'] [0] ['paths']'之後。此外,'$ arr ['overlays'] [0] ['paths']'是一個數組,因此試圖回顯它將不起作用,查看它的結構,您可以執行print_r($ arr ['overlays'] [0] ['paths'])' – MLeFevre

回答

1

因爲這是一個數組,你不能echo它正確的方式。你應該利用print_r

print_r($arr['overlays'][0]['paths']); 

OUTPUT :

Array 
(
    [0] => Array 
     (
      [0] => Array 
       (
        [lat] => 45.37867863632308 
        [lng] => 19.948768615722656 
       ) 

      [1] => Array 
       (
        [lat] => 45.370719925928746 
        [lng] => 19.941558837890625 
       ) 

      [2] => Array 
       (
        [lat] => 45.36227764550136 
        [lng] => 19.92816925048828 
       ) 

      [3] => Array 
       (
        [lat] => 45.359262240003495 
        [lng] => 19.942245483398438 
       ) 

      [4] => Array 
       (
        [lat] => 45.35588479505299 
        [lng] => 19.955806732177734 
       ) 

      [5] => Array 
       (
        [lat] => 45.35974471568275 
        [lng] => 19.958553314208984 
       ) 

      [6] => Array 
       (
        [lat] => 45.36312193024184 
        [lng] => 19.959583282470703 
       ) 

      [7] => Array 
       (
        [lat] => 45.365534102931655 
        [lng] => 19.960613250732422 
       ) 

      [8] => Array 
       (
        [lat] => 45.36529289029106 
        [lng] => 19.96490478515625 
       ) 

      [9] => Array 
       (
        [lat] => 45.37084052080666 
        [lng] => 19.970226287841797 
       ) 

     ) 

) 
1

您陣列結構是這樣的:

array (size=5) 
    'zoom' => int 13 
    'tilt' => int 0 
    'mapTypeId' => string 'hybrid' (length=6) 
    'center' => 
    array (size=2) 
     'lat' => float 45.385912802964 
     'lng' => float 19.9363231698 
    'overlays' => 
    array (size=1) 
     0 => 
     array (size=9) 
      'type' => string 'polygon' (length=7) 
      'title' => string 'Polje 1' (length=7) 
      'content' => string 'Vojvodina' (length=9) 
      'fillColor' => string '#ffbf1a' (length=7) 
      'fillOpacity' => float 0.3 
      'strokeColor' => string '#000' (length=4) 
      'strokeOpacity' => float 0.8 
      'strokeWeight' => int 3 
      'paths' => 
      array (size=1) 
       ... 

轉儲一個數組,使用var_dump($arr)paths位於$arr['overlays'][0]['paths']

+2

你剛剛編輯它;)我發佈答案後。 –

+1

你也編輯了你的答案,我沒有抱怨。 – Patrick

+0

什麼?你剛剛發佈了這個數組結構,並且在我的答案發布之後,你添加了最後一行。 –

1

paths是解碼後的數組,

print_r($arr['overlays'][0]['paths']);