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?

3

5 Answers 5

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);
Sign up to request clarification or add additional context in comments.

2 Comments

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.
But that wouldn't work because it includes the brackets and quotes.
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.

2 Comments

and now how can print the number of objects in each small array
@user2201395 Then you iterate through the array's elements and echo the count() of each element.
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);

Comments

0

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.