How can you check the content of a multidimensional array in PHP and count the entries with a specific value?
To check the content of a multidimensional array in PHP and count the entries with a specific value, you can loop through the array and use conditional statements to check for the specific value. You can then increment a counter variable whenever the specific value is found. Finally, you can return the count of entries with the specific value.
// Sample multidimensional array
$multiArray = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
// Specific value to count
$specificValue = 5;
// Counter variable to store the count
$count = 0;
// Loop through the multidimensional array and count entries with specific value
foreach ($multiArray as $innerArray) {
foreach ($innerArray as $value) {
if ($value == $specificValue) {
$count++;
}
}
}
// Output the count of entries with the specific value
echo "Count of entries with value $specificValue: $count";