Find the number of objects in json array

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

  • count
  • json.length

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

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

How to find the number of objects in my JSON array?

+7
source share
4 answers

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); 
+17
source

You will need to decrypt into PHP arrays before doing any data work.

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 will need to iterate through the array.

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

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

However, this is not necessarily important, because it depends on what you are trying to calculate, what is your goal. This block only counts the number of 1st level layers, regardless of what actual numbers can mean.

+4
source

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

Object (an unordered set of key: value pairs with a ":" character separating the key and the value, separated by commas and enclosed in curly braces; ...)

Wikipedia: JSON

So all you have to do is just count the open braces.

 substr_count($huge , '{'); 

But ... If you save some lines using '{' in json, you cannot do this. So you have to write your own simple parser or regular expression.

But ... eases the path to json_decode. And use a recursive function if you want to get the number 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)); 
0
source

All Articles