PHP counts elements in a multidimensional array

As you can see from the following array, there are three elements that appear on November 18, and two more elements that appear on November 22. Can someone tell me how can I get counters 3 and 2 respectively from this array? Basically, I want to end up with something like this:

November 18, 2011 = 3 elements

November 22, 2011 = 2 items

Of course, dates and the number of different dates will change every time. Here is the array:

Array ( [0] => Array ( [0] => Array ( [2011-11-18 00:00:00] => C ) [1] => Array ( [2011-11-18 00:00:00] => I ) [2] => Array ( [2011-11-18 00:00:00] => S ) ) [1] => Array ( [0] => Array ( [2011-11-22 00:00:00] => C ) [1] => Array ( [2011-11-22 00:00:00] => S ) ) ) 
+8
arrays php multidimensional-array
source share
9 answers

Does this mean what you need?

 $dates = array(array(array("2011-11-18 00:00:00" => C), array("2011-11-18 00:00:00" => I),array ("2011-11-18 00:00:00" => S)), array(array("2011-11-22 00:00:00" => C), array("2011-11-22 00:00:00" => S))); $date_count = array(); // create an empty array foreach($dates as $date) { // go thought the first level foreach($date as $d) { // go through the second level $key = array_keys($d); // get our date // here we increment the value at this date // php will see it as 0 if it has not yet been initialized $date_count[$key[0]]++; } } // show what we have print_r($date_count); 

Print

 Array ( [2011-11-18 00:00:00] => 3 [2011-11-22 00:00:00] => 2 ) 

Note. This assumes that you will always receive data as you structure your array and that each date is formatted the same way. If you cannot assume that each date will be formatted, this will be a simple conversion using the date () function. If you cannot assume that you will get data structured that way, the best way to handle this is probably through a recursive function.

+7
source share

You can use:

 count($array, COUNT_RECURSIVE); 

Count the number of sheets in a tree of nested arrays

+20
source share

the posted answers are correct for your typical example, but I would like to add another solution that will work no matter how many nested arrays you can create. it iterates over the array recursively and counts all the elements in all subarrays.

it returns the total number of elements in the array. in the second argument, you can specify the reference array that will contain the bill for the unique key in the (nested) array.)

Example:

 <?php $deeply_nested = array( 'a' => 'x', 'b' => 'x', 'c' => 'x', 'd' => array( 'a' => 'x', 'b' => 'x', 'c' => array( 'a' => 'x', 'b' => 'x' ), 'e' => 'x' ) ); function count_nested_array_keys(array &$a, array &$res=array()) { $i = 0; foreach ($a as $key=>$value) { if (is_array($value)) { $i += count_nested_array_keys($value, &$res); } else { if (!isset($res[$key]) $res[$key] = 0; $res[$key]++; $i++; } } return $i; } $total_item_count = count_nested_array_keys($deeply_nested, $count_per_key); echo "total count of items: ", $total_item_count, "\n"; echo "count per key: ", print_r($count_per_key, 1), "\n"; 

leads to:

 total count of items: 8 count per key: Array ( [a] => 3 [b] => 3 [c] => 1 [e] => 1 ) 
+3
source share

Assuming your example array is representative:

 foreach ($array as $key => $value) { echo count($value) . "<br />"; } 

Will display the number of arrays in each of the main elements of the array. In your example, this will also be the number of records for each date.

This, of course, does not check the dates themselves

+2
source share

You can use array_walk_recursive() to access all leaf nodes in the array structure.

Something similar to this should work for you:

 <?php $data = array( array( array('2011-11-18 00:00:00' => 'C'), array('2011-11-18 00:00:00' => 'I'), array('2011-11-18 00:00:00' => 'S')), array( array('2011-11-22 00:00:00' => 'C'), array('2011-11-22 00:00:00' => 'S'))); function countleafkeys($value, $key, $userData) { echo "$key\n"; if(!isset($userData[$key])) { $userData[$key] = 1; } else { $userData[$key]++; } } $result = array(); array_walk_recursive($data, 'countleafkeys', &$result); print_r($result); 

Outputs:

 2011-11-18 00:00:00 2011-11-18 00:00:00 2011-11-18 00:00:00 2011-11-22 00:00:00 2011-11-22 00:00:00 Array ( [2011-11-18 00:00:00] => 3 [2011-11-22 00:00:00] => 2 ) 
+1
source share

For your specific $array structure, I think the worst way is using foreach , and then getting the date value and count() from each value:

 $dateCounts = array(); foreach($array as $date) { $dateCounts[key($date[0])] = count($date); } var_dump($dateCounts); 

With $array this gives:

 array(2) { ["2011-11-18 00:00:00"]=> int(3) ["2011-11-22 00:00:00"]=> int(2) } 

If you're looking for a more general way, you can use RecursiveArrayIterator and RecursiveIteratorIterator to move through all the elements of the sheet / sheet value, and then just count the keys:

 $it = new RecursiveIteratorIterator(new RecursiveArrayIterator($array)); $keyCounts = array(); foreach ($it as $key => $value) { isset($keyCounts[$key]) ? $keyCounts[$key]++ : $keyCounts[$key] = 1; } var_dump($keyCounts); 

Hope this helps.

+1
source share

Here is my recursive option:

 $arr = array( '0' => array( '0' => array('2011-11-18 00:00:00' => 'C'), '1' => array('2011-11-18 00:00:00' => 'I'), '2' => array('2011-11-18 00:00:00' => 'S') ), '1' => array( '0' => array('2011-11-22 00:00:00' => 'C'), '1' => array('2011-11-22 00:00:00' => 'S') ), '2' => array( '0' => array( '0' => array('2011-11-22 00:00:00' => 'D') ) ) ); function count_values($array, &$result = array(), $counter = 0) { foreach ($array as $key => $data) { if (is_array($data)) { count_values($data, $result, $counter); } else { array_key_exists($key, $result) ? $result[$key]++ : $result[$key] = 1; } } return $result; } print_r(count_values($arr)); 

This will return:

 Array ( [2011-11-18 00:00:00] => 3 [2011-11-22 00:00:00] => 3 ) 
0
source share
 <?php $count0=count($array[0], COUNT_RECURSIVE)-count($array[0]); $count1=count($array[1], COUNT_RECURSIVE)-count($array[1]); 
0
source share

If you want to consider elements as one-dimensional and two-dimensional, you can try:

 echo 'Size of unidimensional is: '.count($celda).'<br/>'; echo 'Size of bidimensional is: '.count($celda[0]).'<br/>'; 
0
source share

All Articles