Using the variable $arr for your array, you could do this:

$out = array();
foreach (key => $value){
    foreach ($value as $key2 => $value2){
        $index = $key2.'-'.$value2;
        if (array_key_exists($index, $out)){
            $out[$index]++;
        } else {
            index] = 1;
        }
    }
}
var_dump($out);

Output:

Array
(
    [07/11-134] => 2
    [07/11-145] => 2
    [07/12-134] => 1
    [07/12-99] => 1
)

Here's another version that produces it as a multidimensional array:

$out = array();
foreach (key => $value){
    foreach ($value as $key2 => $value2){
        if (array_key_exists($key2, $out) && array_key_exists($value2, key2])){
            key2][$value2]++;
        } else {
            key2][$value2] = 1;
        }
    }
}

Output:

Array
(
    [07/11] => Array
        (
            [134] => 2
            [145] => 2
        )

    [07/12] => Array
        (
            [134] => 1
            [99] => 1
        )

)
Answer from Expedito on Stack Overflow
🌐
PHP
php.net › manual › en › function.array-count-values.php
PHP: array_count_values - Manual
Returns an associative array of values from array as keys and their count as value. Throws E_WARNING for every element which is not string or int. ... Simple way to find number of items with specific values in multidimensional array: <?php $list ...
🌐
Wordpress
vijaymrami.wordpress.com › 2016 › 02 › 03 › count-array-key-values-with-array_count_values-count-and-sizeof-php-functions
Count array key values with array_count_values, count and sizeof PHP functions – PHP Website Development
February 5, 2016 - So (array_sum(array_map('count',$MyArray ))) wiil sum 3 + 1 + 1 + 4 // Only second level values echo (count($MyArray ,COUNT_RECURSIVE)-count($MyArray )); //output 7 ((all elements) - (first elements)) ?> There is a simple script with example for counting rows and columns of a two-dimensional array. <?php $cars = array ( "first" => array( "carname" => "Volvo", "carprice" => 22, "caraverage" => 60, "caroil" => 'castol', "caryear" => 2012 ), "second" => array( "carname" => "BMW", "carprice" => 15, "caraverage" => 70, "caroil" => 'hp', "caryear" => 2013 ), "third" => array( "carname" => "Saab", "c
🌐
DaniWeb
daniweb.com › programming › web-development › threads › 340233 › count-php-array-values-in-multi-dimension-array
Count php array values in multi dimension ... [SOLVED] | DaniWeb
<?php //Multidimensional array of printers. //This can be ANY depth. $printers = array( array('Printer_B', 'IDDHARMAHE'), array('Printer_B', 'IDSIRIKINE'), array('Printer_C', … — mschroeder 251 Jump to Post · Yes, if you're not filtering it for a specific value. In the foreach loop, you can test $key or $value for anything you want to compile the counts etc.
🌐
Matt Doyle
elated.com › home › blog › counting php array elements using count()
Counting PHP Array Elements Using count()
July 23, 2022 - You can calculate the average of the values in the array (in conjunction with array_sum()) PHP makes it easy to count array elements, thanks to its built-in count() function. In this tutorial you’ll learn how to use count() to count the elements in both regular and multidimensional arrays, and how to move through all the elements of an indexed array by using count() and a for loop.
Find elsewhere
🌐
PHP Freaks
forums.phpfreaks.com › php coding › php coding help
Counting values in multidimensional arrays - PHP Coding Help - PHP Freaks
August 7, 2012 - I have an array with something along the lines of.. Array ( [0] => Array ( [name] => Michael ) [1] => Array ( [name] => Michael ) [2] => Array ( [name] => Jordan ) [3] => Array ( [name] => Marie ) [4] => Array ( [name] => Marie ) [5] => Array ( [name] => Michae...
🌐
SitePoint
sitepoint.com › php
How do I count and sort simple multidimensional array like this? - PHP - SitePoint Forums | Web Development & Design Community
May 25, 2011 - Greetings, I have an array like this: Array ( [0] => Array ( [userid] => 1 [name] => Joe1 ) [1] => Array ( [userid] => 1 [name] => Joe1 ) [2] => Array ( [userid] => 2 [name] => Joe2 ) [3] => Array ( [userid] => 3 [name] => Joe3 ) [4] => Array ( [userid] => 2 [name] => Joe2 ) [5] => Array ( ...
🌐
W3Schools
w3schools.com › Php › func_array_count_values.asp
PHP array_count_values() Function
PHP Examples PHP Compiler PHP Quiz PHP Exercises PHP Practice Problems PHP Server PHP Syllabus PHP Study Plan ... array() array_change_key_case() array_chunk() array_column() array_combine() array_count_values() array_diff() array_diff_assoc() array_diff_key() array_diff_uassoc() array_diff_ukey() array_fill() array_fill_keys() array_filter() array_flip() array_intersect() array_intersect_assoc() array_intersect_key() array_intersect_uassoc() array_intersect_ukey() array_key_exists() array_keys() array_map() array_merge() array_merge_recursive() array_multisort() array_pad() array_pop() array_
🌐
PHPBuilder
board.phpbuilder.com › d › 10383945-multidimensional-array-and-count-values
multidimensional array and count values - PHPBuilder Forums
April 3, 2012 - So i am trying to take a while loop and add values to an array. In the end I want to display the Item, #number of each Item and the Total amount for each ite...
🌐
TutorialKart
tutorialkart.com › php › php-count
How to get Length of Array in PHP?
March 2, 2022 - You will mostly use the following form of count() function. ... We can store the returned value in a variable as shown below. ... If you pass multidimensional array, count($arr) returns length of array only in the first dimension.
Top answer
1 of 2
3

this should work with unlimited depth starting from a main array like

$array = array(
    'children' => array( /* ADD HERE INFINITE COMBINATION OF CHILDREN ARRAY */ ),
    'id' => #,
    'name' => '',
    'email' => '',
    'points' => #
);

function recursive_children_points($arr) {
    global $hold_points;
    if (isset($arr['points'])) {
        $hold_points[] = $arr['points'];
    }
    if (isset($arr['children'])) {
        foreach ($arr['children'] as $children => $child) {
            recursive_children_points($child);
        }
    }
    return $hold_points;
}

$points = recursive_children_points($array);
print "<pre>";
print_r($points);
/**
     // OUTPUT
     Array
(
    [0] => 99999
    [1] => 7
    [2] => 4
    [3] => 3
    [4] => 666
    [5] => 43
)
**/
    print "<pre>";
2 of 2
1

In my opinion this calls for recursion because you'll do the same thing over each flat level of the array: add the points.

so you'll need to

  • loop over each array
  • add points found
  • if we find any children, do it again but with children array

while keeping track of levels and jumping out if you reach your limit

With that in mind I thought of the following solution:

<?php

$values = array();

//first
$values[] = array('name'=>'andrej','points'=>1,'children'=>array());
$values[] = array('name'=>'peter','points'=>2,'children'=>array());
$values[] = array('name'=>'mark','points'=>3,'children'=>array());

//second
$values[0]['children'][] = array('name'=>'Sarah','points'=>4,'children'=>array());
$values[2]['children'][] = array('name'=>'Mike','points'=>5,'children'=>array());

//third 
$values[0]['children'][0]['children'][] = array('name'=>'Ron','points'=>6,'children'=>array());

//fourth
$values[0]['children'][0]['children'][0]['children'][] = array('name'=>'Ronny','points'=>7,'children'=>array());

//fifth
$values[0]['children'][0]['children'][0]['children'][0]['children'][] = array('name'=>'Marina','points'=>10,'children'=>array());

//sixth
$values[0]['children'][0]['children'][0]['children'][0]['children'][0]['children'][] = array('name'=>'Pjotr','points'=>400,'children'=>array());


function collect_elements($base, $maxLevel,$child='children',$gather='points', &$catch = array(), $level = 0) {
/*    I pass $catch by reference so that all recursive calls add to the same array
      obviously you could use it straight away but I return the created array as well
  because I find it to be cleaner in PHP (by reference is rare and can lead to confusion)

  $base = array it works on
  $maxLevel = how deep the recursion goes

  $child = key of the element where you hold your possible childnodes
      $gather = key of the element that has to be collected
*/

  $level++;
  if($level > $maxLevel) return; // we're too deep, bail out

  foreach ($base as $key => $elem) {
    // collect the element if available
    if(isset($elem[$gather])) $catch[] = $elem[$gather];

    /*
    does this element's container have children? 
       [$child] needs to be set, [$child] needs to be an array, [$child] needs to have elements itself
    */
    if (isset($elem[$child]) && is_array($elem[$child]) && count($elem[$child])){
       // if we can find another array 1 level down, recurse that as well  
       collect_elements($elem[$child],$maxLevel,$child,$gather, $catch,$level); 
    }
  }
return $catch;
}

print array_sum(collect_elements($values,5)) . PHP_EOL;

collect_elements will collect the element you're interested in (until maximum depth is reached) and append it to a flat array, so that you can act on it upon return. In this case we do an array_sum to get the total of poins collected

Only the first for parameters are interesting:

collect_elements($base, $maxLevel,$child='children',$gather='points'

not optional: $base is the array to work on $maxLevel is the maximum depth the function needs to descend into the arrays optional: $child defines the key of the element that contains the children of current element (array) $gather defines the key of the element that contains what we want to gather

The remaining parameters are just ones used for recursion