D
D
dj_vadim2016-02-26 21:01:56
CodeIgniter
dj_vadim, 2016-02-26 21:01:56

How to count the number of all repetitions of elements in an array?

There is an array of m elements. Inside each element is an array of n elements. How can I count the number of repetitions of element n in any array m? For example, in the first array "3189571" the element "335976945" is repeated, it is numbered 1 and 5. As a result, you need to display all the repeating elements with the number of repetitions in the entire large array.
In our array, the number "335976945" is repeated => 5 times, so we need to display it on the screen.
I think you can check with the in_array () function or do nested loops, but the function will return only one result yes or no, and there can be many repetitions.
Array
(
[3189571] => Array
(
[0] => 128109610
[1] => 335976945
[2] =>
[3] => 144128186
[4] => 308071488
[5] => 335976945
[6] => 323896635
[n] => ...
)
[3189630] => Array
(
[0] => 335976945
[1] = > 341021680
)
[3189566] => Array
(
[0] => 175001040
[1] => 335976945
[2] => 180405054
[3] => 314998956
[4] => 347133292
[n] => ...
)
[ m] => Array
(
[0] =>335976945
[n => ...
)
)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mikhail Osher, 2016-02-26
@dj_vadim

array flatten and then array_count_values.

A
Andrey, 2016-02-26
@VladimirAndreev

$data = [];
array_walk_reqursive(
$input,
function($value) use (&$data) {
if(!isset($data[$value]) {
$data[$value] = 0;
}
++$data[$value];
} );
print_r($data);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question