2013-04-08 152 views
7

我搜索了很多,我发现了很少的方法来查找我的JSON数组的长度。我曾尝试:查找json数组中的对象数

  1. count
  2. json.length

但这些返回1,而不是实际的长度。我想用PHP显示它。

我的JSON是:

$huge = '[{"location":[{"building":["Default Building"],"name":"Default Location"}],"name":"Default Organization"},{"location":[{"building":["test_loc1_building1","test_loc1_building2"],"name":"test location1"},{"building":["test_loc2_building2"],"name":"test location2"}],"name":"test Organization"}]'; 

我如何才能找到我的JSON数组对象的数量?

+3

JSON是一种序列化格式,那么对它进行反序列化和检查呢? – 2013-04-08 06:24:09

+0

在这里你有另一个解决方案,而不使用json_decode。 HTTP://计算器。com/questions/28772904/convert-json-string-to-array-without-json-decode – 2016-03-10 09:19:11

+0

在这里你有另一个解决方案,而不使用json_decode:http://stackoverflow.com/questions/28772904/convert-json-string-to -array-without-json-decode – 2016-03-10 09:20:29

回答

4

在对数据进行任何操作之前,您需要解码为PHP数组。

尝试:

$hugeArray = json_decode($huge, true); // Where variable $huge holds your JSON string. 

echo count($hugeArray); 

如果你需要计算到下层深度,你需要通过数组进行迭代。

例如,如果您希望计算下一层元素的数量,你可以这样做:

foreach ($hugeArray as $key => $value) { 
    echo $key.' - '.count($value); 
} 

然而,这并不一定是有意义的,因为它取决于你正在尝试数,你的目标是什么。不管实际的数字是什么意思,该块只计算下面1层的元素的数量。

+0

现在如何在每个小阵列中打印对象的数量 – user2201395 2013-04-08 06:39:50

+0

@ user2201395然后遍历数组的元素并回显每个元素的count()。 – Stegrex 2013-04-08 08:15:33

0

先解码您的json,之后使用count就可以了。

$huge='[{"location":[{"building":["Default Building"],"name":"Default Location"}],"name":"Default Organization"},{"location":[{"building":["test_loc1_building1","test_loc1_building2"],"name":"test location1"},{"building":["test_loc2_building2"],"name":"test location2"}],"name":"test Organization"}]'; 
$arr = json_decode($huge,true); 
echo count($arr); 
17

您需要decode the json对象再算上其中的元素..

$json_array = json_decode($json_string, true); 
$elementCount = count($json_array); 
+0

您可以通过嵌套'$ elementCount = count(json_decode($ json_string,true));将此缩短为一行;'如果不需要$ json_array作为其他地方的PHP变量。 – AndyD273 2014-12-08 21:07:06

0

对象(关键的无序集合:“:”与值对字符分隔键和的值,以逗号分隔的和包含在大括号; ...)

Wikipedia: JSON

所以,你只需要打开大括号即可。

substr_count($huge , '{'); 

但是...如果你在json中存储了一些带有'{'的字符串,你就不能这样做。这样你就必须编写你自己的简单解析器或正则表达式。

但是...简单的方法来json_decode。如果你想在json中得到全部对象的数目,那么使用递归函数。

function count_objects($value) 
    { 
    if (is_scalar($value) || is_null($value)) 
     $count = 0; 
    elseif (is_array($value)) 
     { 
     $count = 0; 
     foreach ($value as $val) 
      $count += count_objects($val); 
     } 
    elseif (is_object($value)) 
     { 
     $count = 1; 
     $reflection_object = new \ReflectionObject($value); 
     foreach ($reflection_object->getProperties() as $property) 
      { 
      $count +=count_objects($property->getValue($value)); 
      } 
     } 

    return $count; 
    } 

$huge = '[{"location":[{"building":["Default Building"],"name":"Default Location"}],"name":"Default Organization"},{"location":[{"building":["test_loc1_building1","test_loc1_building2"],"name":"test location1"},{"building":["test_loc2_building2"],"name":"test location2"}],"name":"test Organization"}]'; 

echo count_objects(json_decode($huge));