How can you efficiently count the number of arrays that have a specific value in PHP?
To efficiently count the number of arrays that have a specific value in PHP, you can use a loop to iterate through each array and check if the specific value exists in the array. If the value is found, increment a counter variable. Finally, return the count of arrays that contain the specific value.
function countArraysWithValue($arrays, $value) {
$count = 0;
foreach ($arrays as $array) {
if (in_array($value, $array)) {
$count++;
}
}
return $count;
}
$arrays = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
$value = 5;
echo countArraysWithValue($arrays, $value); // Output: 1