使用PHP PDO进行JSON解码

时间:2014-03-06 11:27:31

标签: php json

我有一个JSON字符串,我想解码这个字符串来获取php数组并将其添加到数据库中,但是当我尝试调用数组的某些部分时,我得不到任何东西:

<?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'] ???

3 个答案:

答案 0 :(得分: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']

答案 1 :(得分: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
                )

        )

)

答案 2 :(得分:1)

paths是解码后的数组,

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