Array
(
[0] => Array
(
[prefix] =>
)
[1] => Array
(
[prefix] => +57
)
[2] => Array
(
[prefix] => +268
)
[3] => Array
(
[prefix] => +972
)
[4] => Array
(
[prefix] => +972
)
[5] => Array
(
[prefix] => +33
)
[6] => Array
(
[prefix] => +33
)
[7] => Array
(
[prefix] => +972
)
[8] => Array
(
[prefix] => +509
)
[9] => Array
(
[prefix] =>
)
[10] => Array
(
[prefix] => +33
)
[11] => Array
(
[prefix] => +268
)
[12] => Array
(
[prefix] => +57
)
)
$calls = [
['prefix' => '+57'],
['prefix' => '+268'],
['prefix' => '+972'],
['prefix' => '+972'],
['prefix' => '+33'],
['prefix' => '+33'],
['prefix' => '+972'],
['prefix' => '+509'],
['prefix' => ''],
['prefix' => '+33'],
['prefix' => '+268'],
['prefix' => '+57'],
];
function calcPrefixStat($calls) {
$stat = [];
foreach($calls as $call) {
if (!array_key_exists($call['prefix'], $stat)) {
$stat[$call['prefix']] = 0;
}
$stat[$call['prefix']]++;
}
return $stat;
}
var_dump(calcPrefixStat($calls));
/*
array(6) {
["+57"]=>
int(2)
["+268"]=>
int(2)
["+972"]=>
int(3)
["+33"]=>
int(3)
["+509"]=>
int(1)
[""]=>
int(1)
}
*/
$result = array_reduce($calls, 'myStatCounter', []);
function myStatCounter($stat, $call) {
$prefix = $call['prefix'];
if (empty($stat[$prefix])) $stat[$prefix] = 0;
$stat[$prefix]++;
return $stat;
}