array_count_values


php128 apg

COUNTS all the values of a given ARRAY.





This function returns an associative array of values from $array as keys and their count as value.



<?php

arr array_count_values 
arr $array )


where,

$array The working array

?>

 $array 


The working array.



  1 EXERCISE   

<?php

$arr01 
= [ 2818323218];

echo 
'In the given ARRAY:<br>';

print_r($arr01);

echo 
'<br><br>';

$ctv01 array_count_values($arr01);

foreach(
$ctv01 as $k01 => $v01)
{
    if(
$v01 == 1)
    {
echo 
'The value ' $k01 ' appears ' $v01 ' time.<br>';
    }
    else
    {
echo 
'The value ' $k01 ' appears ' $v01 ' times.<br>';
    }
}
    
?>

 RESULT   

In the given ARRAY:
Array ( [0] => 2 [1] => 8 [2] => 18 [3] => 32 [4] => 32 [5] => 18 [6] => 8 )

The value 2 appears 1 time.
The value 8 appears 2 times.
The value 18 appears 2 times.
The value 32 appears 2 times.


  2 EXERCISE   

<?php

$arr02 
= [ "K" => 2
                 
"L" => 8"M" => 18
                 
"N" => 32"O" => 32
                 
"P" => 18"Q" => ];

echo 
'In the given ARRAY:<br>';

print_r($arr02);

echo 
'<br><br>';

$ctv02 array_count_values($arr02);

foreach(
$ctv02 as $k02 => $v02)
{
    if(
$v02 == 1)
    {
echo 
'The value ' $k02 ' appears ' $v02 ' time.<br>';
    }
    else
    {
echo 
'The value ' $k02 ' appears ' $v02 ' times.<br>';
    }
}    

?>

 RESULT   

In the given ARRAY:
Array ( [K] => 2 [L] => 8 [M] => 18 [N] => 32 [O] => 32 [P] => 18 [Q] => 8 )

The value 2 appears 1 time.
The value 8 appears 2 times.
The value 18 appears 2 times.
The value 32 appears 2 times.


  3 EXERCISE   

<?php

$arr03 
= [ "A" => [2818 ], "B" => [ 3232 ] ];

$ctv03 array_count_values($arr03);

var_export($ctv03);

?>

 RESULT   

Warning: array_count_values(): Can only count string and integer values, ...

Warning: array_count_values(): Can only count string and integer values, ...


  4 EXERCISE   

<?php

$arr04 
= ['South America' => 'Brazil'
                           
'Europe' => 'Portugal',
                                
'Asia' => 'Japan'];  

$ctv04 array_count_values($arr04);

var_export($ctv04);

?>