I have foreach loop like this
foreach($destinations as $destination)
{
if($destination=="abc")
{
$msg = "Yes";
}
else
{
$msg = "No";
}
}
How can I count number of "Yes" and "No" generated by "if statement" outside "foreach loop"?
I have foreach loop like this
foreach($destinations as $destination)
{
if($destination=="abc")
{
$msg = "Yes";
}
else
{
$msg = "No";
}
}
How can I count number of "Yes" and "No" generated by "if statement" outside "foreach loop"?
Just try with the following example :
<?php
$destinations = array('abc','def','ghi');
foreach($destinations as $destination)
{
if($destination=="abc")
{
$msg = "Yes";
$msg_yes_counter[]= "I'm in";
}
else
{
$msg = "No";
$msg_no_counter[]= "I'm in";
}
}
echo "YES -> My Count is :".count($msg_yes_counter);
echo "NO -> My Count is :".count($msg_no_counter);
?>
Use the array_count_values() function, no need for a loop at all then.
array_count_values($destinations);
"abc" elements in $destinations?