I have an array like this
[0] => Array
(
[display?] => 'no'
[field] => value
field] => value
field] => value
field] => value
field] => value
)
[1] => Array
(
[display?] => 'no'
[field] => value
field] => value
field] => value
field] => value
field] => value
)
[2] => Array
(
[display?] => 'no'
[field] => value
field] => value
field] => value
field] => value
field] => value
)
[3] => Array
(
[display?] => 'yes'
[field] => value
field] => value
field] => value
field] => value
field] => value
)
[4] => Array
(
[display?] => 'no'
[field] => value
field] => value
field] => value
field] => value
field] => value
)
What I'm trying to do is loop through the arrays and find an array with [display?] => 'yes. If i find one then use that array, if not use the first array I've looped over. I've tried using a foreach loop mixed with an if statement but i cant seem to find a way to get it to work.
foreach($event as $event_item) {
// Check if has "display on front page"
$checked = $event_item['display_on_front_page'];
if($checked == 'yes') {
// Show this $event_item
} else {
// Show the first $event_item in the loop
}
}
I'm relatively new to this.
is this what you want?
$result = null;
foreach($inputArray as $value)
{
if($value['display?'] == 'yes')
{
$result = $value;
break;
}
}
if($result == null)
{
$result = $inputArray[0];
}
//$result contains the value you want?
At the end, $result will either contain the first item in $inputArray that was == 'yes' or it will contain the first item in $inputArray.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments