PHP Count number of occurrences in numeric array -
i have php array , have dumped below using zend_debug:
$ids = array(13) { [0] => string(1) "7" [1] => string(1) "8" [2] => string(1) "2" [3] => string(1) "7" [4] => string(1) "8" [5] => string(1) "4" [6] => string(1) "7" [7] => string(1) "3" [8] => string(1) "7" [9] => string(1) "8" [10] => string(1) "3" [11] => string(1) "7" [12] => string(1) "4" } i trying how many times each number occurs in array , output array.
i have tried using array_count_values($ids) outputs in order of occurred cant total times numbers occur. gives me below output:
array(5) { [7] => int(5) [8] => int(3) [2] => int(1) [4] => int(2) [3] => int(2) } i can see above array 7 occurs 5 times can access when loop through array!
any thoughts?
cheers
j.
you can access data want this:
$ids = array( ...); $array = array_count_values( $ids); foreach( $array $number => $times_number_occurred) { echo $number . ' occurred ' . $times_number_occurred . ' times!'; } output:
7 occurred 5 times! 8 occurred 3 times! 2 occurred 1 times! 4 occurred 2 times! 3 occurred 2 times!
Comments
Post a Comment