Write a PHP script to count the total number of times a specific value appears in an array

  • برمجة بي اتش بي

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)
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...