Write a PHP script to count the total number of times a specific value appears in an array
<?php function count_array_values($my_array, $match) { $count = 0; foreach ($my_array as $key => $value) { if ($value == $match) { $count++; } } return $count; } $colors = array("c1"=>"Red", "c2"=>"Green", "c3"=>"Yellow", "c4"=>"Red"); echo "\n"."Red color appears ".count_array_values($colors, "Red"). " time(s)."."\n"; ?>
Sample Output:
Red color appears 2 time(s)
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Sample Output:
need an explanation for this answer? contact us directly to get an explanation for this answer