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?

Mischa
  • 42,876
  • 8
  • 99
  • 111
user2201395
  • 179
  • 1
  • 2
  • 8

5 Answers5

34

You need to decode the json object and then count the elements in it ..

 $json_array  = json_decode($json_string, true);
 $elementCount  = count($json_array);
AndyD273
  • 7,177
  • 12
  • 54
  • 92
alwaysLearn
  • 6,882
  • 7
  • 39
  • 67
  • You can shorten this to one line by nesting `$elementCount = count(json_decode($json_string, true));` If you don't need $json_array as a PHP variable elsewhere. – AndyD273 Dec 08 '14 at 21:07
  • But that wouldn't work because it includes the brackets and quotes. – Agil Apr 23 '19 at 11:19
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.

Stegrex
  • 4,004
  • 1
  • 17
  • 19
0

First decode your json and after that use count on it.

$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);
Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100
0

We can use ".sizeof()" function. So

-1

Object (an unordered collection of key:value pairs with the ':' character separating the key and the value, comma-separated and enclosed in curly braces; ...)

Wikipedia: JSON

So, all you have to do are just count opened curly braces.

substr_count($huge , '{');

But... If you are storing some strings with '{' in json you can't do it. In that way you have to write your own simple parser or regular expression.

But... the easies way to json_decode. And use recursive function if you want to get count of all objects in 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));
sectus
  • 15,605
  • 5
  • 55
  • 97