Reputation: 117
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.
Upvotes: 1
Views: 749
Reputation: 36
No need to go through such a lenghty process a the best solution may be
if (array_search('yes', array_column($arrayvar, 'display'))) {
Do whatever need;
}
Upvotes: 0
Reputation: 75
php function array_search . . http://php.net/manual/en/function.array-search.php
Hope this helps.
<?php
function multidimensional_search($haystack, $needle) {
if (empty($needle) || empty($haystack)) {
return false;
}
foreach ($haystack as $key => $value) {
$exists = true;
foreach ($needle as $skey => $svalue) {
$exists = ($exists && IsSet($haystack[$key][$skey]) && $haystack[$key][$skey] == $svalue);
}
if($exists){ return $key; }
}
return false;
}
$some_array = array();
$some_array[] = array
('field'=>'some value', 'display'=>'no');
$some_array[] = array
('field'=>'some value', 'display'=>'yes');
$some_array[] = array
('field'=>'some value', 'display'=>'no');
$display = multidimensional_search($some_array, array('display'=>'yes'));
$display = ( $display > -1 ) ? $display : 0;
echo 'display = ' . $display;
Upvotes: 0
Reputation: 1858
You want something like this :
$resultArray = [];
foreach($myArray as $array) {
if($array['display?'] === 'yes') {
$resultArray[] = $array;
break;
}
}
if(empty($resultArray) {
$resultArray[] = $myArray[0];
}
Upvotes: -1
Reputation: 2896
Try something like
<?php
$arrayKeys = array_keys($myArray);
$arrayIndex = 0;
for($i = 0; $i < sizeof($arrayKeys) && $arrayIndex === 0; $i++) {
if ($myArray[$arrayKeys[$i]]['display?'] === 'yes') {
$arrayIndex = $i;
}
}
$arrayToUse = $myArray[$arrayKeys[$arrayIndex]];
?>
Upvotes: 0
Reputation: 96159
There are most likely way cleverer solutions, but ...
<?php
$src = array(
'mary'=>array('display'=>'no', 'a'=>'a1', 'b'=>'b1'),
'had'=>array('display'=>'yes', 'a'=>'a2', 'b'=>'b2'),
'a'=>array('display'=>'no', 'a'=>'a3', 'b'=>'b3'),
'little'=>array('display'=>'no', 'a'=>'a4', 'b'=>'b4'),
'lamb'=>array('display'=>'no', 'a'=>'a5', 'b'=>'b5')
);
$usethisone = null;
foreach( $src as $e ) {
if ( 'yes'===$e['display'] ) {
// first element to fulfill the condition
// store it and break the loop
$usethisone = $e;
break;
}
else if ( is_null($usethisone) ) {
// this can only apply for the first element in the array
// otherwise $usethisone wouldn't be null ...or the loop would have been left, because the first element had display=yes
$usethisone = $e;
}
}
var_export($usethisone);
Upvotes: 0
Reputation: 845
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.
Upvotes: 2