PHP - Count Frequency of Array Values

PHP - count frequency of array values

Sort them after counting them with arsort()

$result = array_count_values(explode(',', $array));
arsort($result);

Array
(
[joe] => 4
[1] => 3
[2] => 2
[4] => 2
[3] => 2
[9] => 1
[8] => 1
[5] => 1
[6] => 1
[7] => 1
)

How to I count the frequency of an array (PHP)

I think that something like this, using krsort() instead of arsort(), would give the desired output:

<?php
$array = [15, 17, 17];
$scores = array_count_values($array);
krsort($scores); // This will order it by score descending.

echo "<table>\n";
echo "<tr><th>Score</th><th>Frequency</th></tr>\n";
foreach ($scores as $value => $count) {
echo "<tr><td>" . $value . "</td><td>" . $count . "</td></tr>\n";
}
echo "</table>\n";

PHP - How to count all the values of an array with limited ranges?

use this for 10 equal segments

    $array = array(10,25,47,14,45,58,25,29,15,36,45,15,25,27,34);

$step = (max($array) - min($array)) / 10;

$result = [];
for ($i = min($array); $i < max($array) - $step; $i += $step) {
$res = array_filter($array, function($v) use ($i, $step) {
return ($i <= $v) && $v < $i +$step;
});
$result[$i . '-' . ($i + $step)] = $res;
}

Return:

  "10 - 14.8" => 2
"14.8 - 19.6" => 2
"19.6 - 24.4" => 0
"24.4 - 29.2" => 5
"29.2 - 34" => 0
"34 - 38.8" => 2
"38.8 - 43.6" => 0
"43.6 - 48.4" => 3
"48.4 - 53.2" => 0
"53.2 - 58" => 0

How do I count occurrence of duplicate items in array

array_count_values, enjoy :-)

$array = array(12,43,66,21,56,43,43,78,78,100,43,43,43,21);
$vals = array_count_values($array);
echo 'No. of NON Duplicate Items: '.count($vals).'<br><br>';
print_r($vals);

Result:

No. of NON Duplicate Items: 7
Array
(
[12] => 1
[43] => 6
[66] => 1
[21] => 2
[56] => 1
[78] => 2
[100] => 1
)

count the number of occurrence of each element in an array (in php)

As mentioned in the comments array_count_values() is the optimal solution in php. But you must write it out aka show your understanding on how to search an array its rather simple as well.

$a=['a','b','a','c','a','d'];
$output=[];

for($i = 0; $i < count($a); $i++){
if(!isset($output[$a[$i]])){
$output[$a[$i]] = 1;
}

else{
$output[$a[$i]] = $output[$a[$i]] + 1;
}
}

var_dump($output);

//output
array(4) {
["a"] => int(3)
["b"] => int(1)
["c"] => int(1)
["d"] => int(1)
}

Calculate Document Frequency in Multidimensinal Array PHP

Use array_count_values function

$terms = array_count_values(array_column($arr, 'term'));

foreach($arr as &$x) {
$x['doc_frequency'] = $terms[$x['term']];
}

demo

Count occurrences of a value in a column of an array of object arrays

$count = 0;
foreach ($array as $item) {
if ($item->type === 'photo') {
$count++;
}
}

PHP: Count and display the value of a multidimensional array

Simple enough here is an example:

<?php

/*Create the array for example */
$data[0]['city'] = "LONDON";
$data[0]['country'] = "ENGLAND";
$data[1]['city'] = "LONDON";
$data[1]['country'] = "ENGLAND";
$data[2]['city'] = "LONDON";
$data[2]['country'] = "ENGLAND";
$data[3]['city'] = "PARIS";
$data[3]['country'] = "FRANCE";
$data[4]['city'] = "LIVERPOOL";
$data[4]['country'] = "ENGLAND";
$data[5]['city'] = "ROME";
$data[5]['country'] = "ITALY";
$data[6]['city'] = "ROME";
$data[6]['country'] = "ITALY";
$data[7]['city'] = "PARIS";
$data[7]['country'] = "FRANCE";
$data[8]['city'] = "BRISTOL";
$data[8]['country'] = "ENGLAND";

/* print the array for show
echo '<pre>';
print_r($data);*/

foreach($data as $val){
if(!isset($newData[$val['country']][$val['city']])){
$newData[$val['country']][$val['city']] = 1;
} else {
++$newData[$val['country']][$val['city']];
}
$sorted["{$val['country']} - {$val['city']}"] = $newData[$val['country']][$val['city']];
}

/* print created array */
echo '<pre>';
print_r($newData);

/* print the sorted array */
arsort($sorted);
print_r($sorted);

Will return:

Array
(
[ENGLAND] => Array
(
[LONDON] => 3
[LIVERPOOL] => 1
[BRISTOL] => 1
)

[FRANCE] => Array
(
[PARIS] => 2
)

[ITALY] => Array
(
[ROME] => 2
)

)
Array
(
[ENGLAND - LONDON] => 3
[FRANCE - PARIS] => 2
[ITALY - ROME] => 2
[ENGLAND - LIVERPOOL] => 1
[ENGLAND - BRISTOL] => 1
)

Edit - Added select from $newData:

Example of select - just to illustrate how to loop and get your values and keys:

?><select id="city" name="city"><?php
foreach($newData as $country => $cities){

foreach($cities as $city => $quantity){
?>
<option id="<?php echo $city;?>" value='<?php echo "$country-$city";?>' ><?php echo $city;?></option>
<?php
}

}
?></select><?php

Will return:

Sample Image

Which is:

<select id="city" name="city">        
<option id="LONDON" value="ENGLAND-LONDON">LONDON</option>
<option id="LIVERPOOL" value="ENGLAND-LIVERPOOL">LIVERPOOL</option>
<option id="BRISTOL" value="ENGLAND-BRISTOL">BRISTOL</option>
<option id="PARIS" value="FRANCE-PARIS">PARIS</option>
<option id="ROME" value="ITALY-ROME">ROME</option>
</select>

Edit - if you want this: all data sorted and countries

Sample Image
Do this:

add

$sorted2[$val['city']] = $newData[$val['country']][$val['city']];
$countries[$val['city']] = $val['country'];

to the first foreach after $sorted...

after the loop add:

arsort($sorted2);

And in the select loop do this:

?><select id="city" name="city"><?php
foreach($sorted2 as $city => $quantity){
?>
<option id="<?php echo $city;?>" value='<?php echo $city;?>' ><?php echo "$city $countries[$city] ($quantity)";?></option>
<?php
}
?></select><?php


Related Topics



Leave a reply



Submit