php – Find the number of objects in a json array-ThrowExceptions

Exception or error:

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?

How to solve:

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

Answer:

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.

Answer:

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

Answer:

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

Answer:

We can use “.sizeof()” function.
So

Leave a Reply

Your email address will not be published. Required fields are marked *