14

I have searched a lot and I found few methods to find the length of my JSON array. I have tried:

  1. count
  2. json.length

but these return 1, instead of the actual length. I want to show it using PHP.

My json is:

$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"}]';

How can I find the number of objects in my JSON array?

4

5 回答 5

34

您需要解码 json对象,然后计算其中的元素..

 $json_array  = json_decode($json_string, true);
 $elementCount  = count($json_array);
于 2013-04-08T06:35:50.913 回答
6

You'll need to decode into PHP arrays before you do any work with the data.

Try:

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

echo count($hugeArray);

If you need to count to a lower depth, you'll need to iterate through the array.

For example, if you want to count the number of elements in the next layer, you can do:

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

However, this isn't necessarily meaningful because it depends on what you are trying to count, what your goal is. This block only counts the number of elements 1 layer underneath, regardless of what the actual numbers could mean.

于 2013-04-08T06:26:04.127 回答
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);
于 2013-04-08T06:27:07.740 回答
0

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

维基百科: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));
于 2013-04-08T07:07:37.743 回答
0

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

于 2020-03-13T12:33:18.793 回答