在一个 JSON 数组中查找对象的数量

14

我已经搜索了很多,并找到了一些方法来查找我的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是一种序列化格式,那么对它进行反序列化并检查呢? - Benjamin Gruenbaum
这里有另一种解决方案,不使用json_decode。http://stackoverflow.com/questions/28772904/convert-json-string-to-array-without-json-decode - Luis Gerardo Mateos
这里有另一种解决方案,不使用json_decode:http://stackoverflow.com/questions/28772904/convert-json-string-to-array-without-json-decode - Luis Gerardo Mateos
5个回答

34
你需要解码json对象,然后计算其中的元素数量。
 $json_array  = json_decode($json_string, true);
 $elementCount  = count($json_array);

如果您不需要在 PHP 中的其他地方使用 $json_array,那么可以通过嵌套 $elementCount = count(json_decode($json_string, true)); 将其缩短为一行。 - AndyD273
但那不会起作用,因为它包括括号和引号。 - Agil

6

在对数据进行任何操作之前,您需要将其解码为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);
}

然而,这并不一定有意义,因为它取决于您要计算什么、您的目标是什么。

这个块只计算下面一层的元素数量,无论实际数字意味着什么。


现在我们该如何打印每个小数组中的对象数量? - user2201395
然后您可以遍历数组的元素并输出每个元素的count()。 - Stegrex

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);

0
我们可以使用".sizeof()"函数。因此


-1
对象(一组无序的键值对,用冒号“:”分隔键和值,逗号分隔并括在花括号内; ...)。 维基百科: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));

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接