A bit late but this is a clean way to write it.

$totalTickets = array_sum(array_map("count", $tickets));

Assuming $tickets is your multi-dimensional array.

I've expanded the code to give an explained example because array_map might be new to some

$tickets = array(
    "type1" => array(
        "ticket1" => array(),
        "ticket2" => array(),
        "ticket3" => array(),
    ),
    "type2" => array(
        "ticket4" => array(),
        "ticket5" => array(),
        "ticket6" => array(),
        "ticket7" => array(),
    ),
    "type3" => array(
        "ticket8" => array()
    )
);

// First we map count over every first level item in the array
// giving us the total number of tickets for each type.
$typeTotals = array_map("count", $tickets);

// print_r($typeTotals);
// $type_totals --> Array (
//                       [type1] => 3,
//                       [type2] => 4,
//                       [type3] => 1 
//                  )

//Then we sum the values of each of these
$totalTickets = array_sum($typeTotals);

print($totalTickets);
// $totalTickets --> 8

So because we don't care about the intermediate result of each type we can feed the result into array_sum

$totalTickets = array_sum(array_map("count", $tickets));
Answer from bigkm on Stack Overflow
🌐
PHP
php.net › manual › en › function.array-count-values.php
PHP: array_count_values - Manual
... Simple way to find number of ... 'userId' => 5], ['id' => 3, 'userId' => 6], ]; $userId = 5; echo array_count_values(array_column($list, 'userId'))[$userId]; // outputs: 2 ?>...
🌐
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.
🌐
Stack Overflow
stackoverflow.com › questions › 37925900 › count-multidimensional-array-keys-with-specific-value
php - Count multidimensional array keys with specific value - Stack Overflow
June 21, 2016 - Better do ` $count[$array['ruleid']] = isset( $count[$array['ruleid']]) ? $count[$array['ruleid']]+1:1;` first time the key is set to NULL PHP 5.6 -- ignore this !!
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 13872692 › count-specific-values-in-a-multidimensional-array
php - count specific values in a multidimensional array - Stack Overflow
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($nested_arrays, $count_per_key); echo "count per key: ", print_r($count_per_key), "\n";
🌐
Stack Overflow
stackoverflow.com › questions › 41408605 › how-do-i-count-same-values-in-an-multidimensional-array › 41408634
php - How do I count same values in an multidimensional array? - Stack Overflow
This code does the following: First it is creating a new Array $countItemsById. After that it loops trough your existing array and increases the value in the new array by 1 for the index (key) that has the value of the id.
🌐
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 ...
Top answer
1 of 3
1

Using a couple of useful builtin functions you can do

$in = [
        ['CHICAGO', '14', 'MUFFIN A LINE', 'MUFA-D', 'Arm Bearings - Check for play, lubricate if needed.'],
        ['CHICAGO', '14', 'MUFFIN A LINE', 'MUFA-D', 'Linkage - Check for wear and broken links.'],
        ['MEMPHIS', '05', 'BREADING LINE 1', 'BRD1-IS', 'Gear Box oil level'],
        ['MEMPHIS', '05', 'BREADING LINE 1', 'BRD1-IS', 'Bearings visual inspection'],
        ['MEMPHIS', '05', 'BREADING LINE 1', 'BRD1-IS', 'Electrical Plug condition'],
        ['CHICAGO', '02', 'JBC LINE 2', 'JBC-BAK-B', 'Plate separator shaft']
];

function grab3($occ)
{
    return $occ[3];
}

print_r(array_count_values(array_map('grab3', $in)));

And the result is an array where the values are now the key of an array and the value is the count


Array
(
    [MUFA-D] => 2
    [BRD1-IS] => 3
    [JBC-BAK-B] => 1
)
2 of 3
0

You can just loop over the array and use the indexes value as a key in an associative array to count. Then $result will contain your desired results.

$data = [
    ['CHICAGO', '14', 'MUFFIN A LINE', 'MUFA-D', 'Arm Bearings - Check for play, lubricate if needed.',],
    ['CHICAGO', '14', 'MUFFIN A LINE', 'MUFA-D', 'Linkage - Check for wear and broken links.',],
    ['MEMPHIS', '05', 'BREADING LINE 1', 'BRD1 - IS', 'Gear Box oil level',],
    ['MEMPHIS', '05', 'BREADING LINE 1', 'BRD1 - IS', 'Bearings visual inspection',],
    ['MEMPHIS', '05', 'BREADING LINE 1', 'BRD1 - IS', 'Electrical Plug condition',],
    ['CHICAGO', '02', 'JBC LINE 2', 'JBC - BAK - B', 'Plate separator shaft',],
];

$result = [];
foreach ($data as $item) {
    $index = $item[3];
    $result[$index] = isset($result[$index]) ? $result[$index] + 1 : 1;
}

print_r($result);

Will print

Array
(
    [MUFA-D] => 3
    [BRD1 - IS] => 4
    [JBC - BAK - B] => 2
)
🌐
W3Schools
w3schools.com › php › func_array_count.asp
PHP count() Function
<?php $cars=array ( "Volvo"=>array ( "XC60", "XC90" ), "BMW"=>array ( "X3", "X5" ), "Toyota"=>array ( "Highlander" ) ); echo "Normal count: " . count($cars)."<br>"; echo "Recursive count: " .