从json数组php获取特定数据

时间:2018-11-15 17:52:41

标签: php arrays json

我正在尝试从外部API接收的数组中获取字符串,但到目前为止我还没有碰到任何运气。

这是数组:

stdClass Object 
( 
[type] => FeatureCollection
[totalFeatures] => 1
[features] => Array
  (
    [0] => stdClass Object
      (
        [type] => Feature
        [id] => perceel.4304705
        [geometry] => stdClass Object
          (
            [type] => Polygon
            [coordinates] => Array
              (
                [0] => Array
                  (
                    [0] => Array
                      (
                        [0] => 135608.866 
                        [1] => 444767.317
                      ) 
                    [1] => Array
                      (
                        [0] => 135638.627
                        [1] => 444759.41
                      )
                    [2] => Array
                      (
                        [0] => 135640.736
                        [1] => 444758.912
                      )
                    [3] => Array
                      (
                        [0] => 135642.048
                        [1] => 444758.752
                      )
                    [4] => Array
                      (
                        [0] => 135643.376
                        [1] => 444758.88
                      )
                    [5] => Array
                      (
                        [0] => 135644.544
                        [1] => 444759.104
                      )
                    [6] => Array
                      (
                        [0] => 135645.568
                        [1] => 444759.552
                      )
                    [7] => Array
                      (
                        [0] => 135646.512
                        [1] => 444760.064
                      )
                    [8] => Array
                      (
                        [0] => 135647.472
                        [1] => 444760.896
                      )
                    [9] => Array 
                      (
                        [0] => 135648.176
                        [1] => 444761.664
                      )
                    [10] => Array
                      (
                        [0] => 135648.72
                        [1] => 444762.464
                      )
                    [11] => Array
                      (
                        [0] => 135649.136
                        [1] => 444763.552
                      )
                    [12] => Array
                      (
                        [0] => 135650.208
                        [1] => 444766.528
                      )
                    [13] => Array
                      (
                        [0] => 135653.968
                        [1] => 444777.568
                      ) 
                    [14] => Array
                      (
                        [0] => 135654.464
                        [1] => 444779.36
                      )
                    [15] => Array
                      (
                        [0] => 135660.672
                        [1] => 444802.784
                      )
                    [16] => Array
                      (
                        [0] => 135667.088
                        [1] => 444826.912
                      ) 
                    [17] => Array
                      (
                        [0] => 135669.351
                        [1] => 444835.724
                      )
                    [18] => Array
                      (
                        [0] => 135673.947
                        [1] => 444852.99
                      )
                    [19] => Array
                      (
                        [0] => 135680.226
                        [1] => 444851.14
                      )
                    [20] => Array
                      (
                        [0] => 135687.926
                        [1] => 444871.704
                      )
                    [21] => Array
                      (
                        [0] => 135688.078
                        [1] => 444872.023
                      )
                    [22] => Array
                      (
                        [0] => 135688.056
                        [1] => 444872.376
                      )
                    [23] => Array
                      (
                        [0] => 135687.892
                        [1] => 444872.648
                      )
                    [24] => Array
                      (
                        [0] => 135687.665
                        [1] => 444872.796
                      )
                    [25] => Array
                      (
                        [0] => 135681.376
                        [1] => 444874.368
                      )
                    [26] => Array
                      (
                        [0] => 135638.985
                        [1] => 444885.681
                      )
                    [27] => Array
                      (
                        [0] => 135624.48
                        [1] => 444831.424
                      )
                    [28] => Array
                      (
                        [0] => 135619.072
                        [1] => 444811.2
                      )
                    [29] => Array
                      (
                        [0] => 135613.504
                        [1] => 444790.432
                      )
                    [30] => Array
                      (
                        [0] => 135607.894
                        [1] => 444768.679
                      )
                    [31] => Array
                      (
                        [0] => 135608.866
                        [1] => 444767.317
                      ) 
                    )
                  )
                )
              [geometry_name] => begrenzingperceel
              [properties] => stdClass Object
                (
                  [lokaalID] => 340113504
                  [logischtijdstipOntstaan] => 2009-03-27T23:59:59.000
                  [logischtijdstipVervallen] =>
                  [kadastraleGemeenteCode] => VAN00
                  [sectie] => B
                  [kadastralegrootte] => 5340
                  [perceelnummer] => 3857
                  [perceelnummer_rotatie] => 0
                  [perceelnummer_deltax] => 0
                  [perceelnummer_deltay] => 0
                  [plaatscoordinaten] => stdClass Object
                    (
                      [type] => Point
                      [coordinates] => Array
                        (
                          [0] => 135644.749
                          [1] => 444828.56
                        )
                      )
                    )
                  )
                )
              [crs] => stdClass Object
                (
                  [type] => name
                  [properties] => stdClass Object 
                    (
                      [name] => urn:ogc:def:crs:EPSG::28992
                    )
                  )
                )

我正在尝试获取“ [kadastralegrootte] => 5340”,而不是“ 5340”部分。

我尝试过这样的事情:

$json = json_decode($result, true);    
$grootte = $json->features->properties->kadastralegrootte;
echo "$grootte"; 

其中$ result是一个json文件。 到目前为止没有运气。如果有人看看,我将不胜感激。

2 个答案:

答案 0 :(得分:1)

怎么回事?

$json = json_decode($result, true);    
$val= $json["features"][0]["geometry"]["properties"]["kadastralegrootte"];
echo "$val"; 
给我json字符串后,

编辑

$data = json_decode($result);
$value = $data->features[0]->properties->kadastralegrootte;
echo $value;

我只是删除了真正的第二个参数,所以它返回一个对象。尝试一下: https://repl.it/@parzibyte/OlivedrabEnormousCosmos

答案 1 :(得分:0)

https://justpaste.it/6u9le处查看了代码示例之后,我认为您可以通过两种不同的方式来实现它,例如将其解码为 array 并使用方括号符号[] OR 解码为对象,但是您必须使用箭头运算符->对其进行访问。希望能有所帮助:)

// decode it as array
$json = json_decode($result,1); // using 2nd parameter true
$grootte = $json['features'][0]['properties']['kadastralegrootte'];
echo "$grootte";

// decode it as object
$json = json_decode($result); // not using 2nd parameter 
$grootte = $json->features[0]->properties->kadastralegrootte;
echo "$grootte";