$a1=array("first", "second", "third","fourth","fifth","second","third");
$new_array=array_count_values($a1);
while (list ($key, $val) = each ($new_array)) {
echo "$key -> $val <br>";
}
The output of above code will be
first -> 1
second -> 2
third -> 2
fourth -> 1
fifth -> 1
Syntax
array_count_values ($input_array )
Parameter | DESCRIPTION |
---|---|
$input_array | Required : Input array of which frequency of values to be calculated |
$array = [1, "apple", "orange", "apple", 1, 2];
$result = array_count_values($array);
print_r($result);
// Output: Array ( [1] => 2 [apple] => 2 [orange] => 1 [2] => 1 )
function flatten_array($array) {
$flat_array = [];
foreach ($array as $item) {
if (is_array($item)) {
$flat_array = array_merge($flat_array, flatten_array($item));
} else {
$flat_array[] = $item;
}
}
return $flat_array;
}
$array = [1, [1, 2], "apple", [1, "apple"]];
$flat_array = flatten_array($array);
$result = array_count_values($flat_array);
print_r($result);
// Output: Array ( [1] => 3 [2] => 1 [apple] => 2 )
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.
periyasamy | 01-01-2010 |
Hi Friends,...i am new to php,..i have problem taking list box value.While loop i have 5 select box, need to pass whole value into single submissio n ,..how to pass,,please help me thanks periyasamy |