【问题标题】:Find the number of objects in a json array查找 json 数组中的对象数
【发布时间】:2013-04-08 06:22:57
【问题描述】:

我进行了很多搜索,但发现很少有方法可以找到我的 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 数组中的对象数量?

【问题讨论】:

标签: php json


【解决方案1】:

你需要decode the json对象,然后计算其中的元素..

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

【讨论】:

  • 如果你不需要 $json_array 作为 PHP 变量,你可以通过嵌套 $elementCount = count(json_decode($json_string, true)); 将其缩短为一行。
  • 但这行不通,因为它包含括号和引号。
【解决方案2】:

在对数据进行任何操作之前,您需要将其解码为 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 层的元素数量,而不管实际数字是什么意思。

【讨论】:

  • 现在如何打印每个小数组中的对象数量
  • @user2201395 然后遍历数组的元素并回显每个元素的 count()。
【解决方案3】:

首先解码您的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);

【讨论】:

    【解决方案4】:

    对象(键:值对的无序集合,键和值之间用 ':' 字符分隔,以逗号分隔并用大括号括起来;...)

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

    【讨论】:

      【解决方案5】:

      我们可以使用“.sizeof()”函数。 所以

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-07-22
        • 2014-05-13
        • 1970-01-01
        • 2019-04-08
        • 2022-08-03
        • 2014-08-20
        • 2017-11-25
        相关资源
        最近更新 更多