如何计算关联/嵌套数组的叶元素

I have been trying to count the last leaf node elements in a array. Im thinking along the lines of:

  • Getting a simple array of leaf elements from the testArray using: 'array_walk_recursive'.
  • Count elements in the new array using: 'array_count_values'.

Im unsure how to get a simple list array from 'array_walk_recursive', i just get a long string of values....or is there a better way of achieving this result?

DESIRED RESULT:

flammable = 1
irritant = 2 
toxic = 3

PHP:

$testArray = Array
(
    [0] => Array
    (
        [0] => toxic
        [1] => irritant
        [3] => flammable
    )

    [1] => Array
    (
        [0] => toxic
        [1] => irritant
    )

    [2] => Array
    (
        [0] => toxic
    )
);

array_walk_recursive($testArray, function(&$value) 
{
    echo 'string = '.$value;
    print_r(newArray);              //How can i get this new array list?
});

 $counts = array_count_values($newArray); //and use this to count values?

Try this, the numbers should show up in the $groups array.

$groups = array();

array_walk_recursive($testArray, function(&$value) use (&$groups)
{
    if (isset($groups[$value])) {
        $groups[$value]++;
    } else {
        $groups[$value] = 1;
    }
});

print_r($groups);