如何遍历多维数组并检查值,然后显示该数组,或使用数组中的第一个数组


How can I loop through a Multidimensional array and check for a value, then display that array, or use the first in the array

我有一个这样的数组

[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
    )

我想做的是遍历数组并找到一个带有 [display?] => '是的。如果我找到一个,那么使用该数组,如果不使用该我循环的第一个数组。我尝试使用与 if 语句混合的 foreach 循环,但我似乎找不到让它工作的方法。

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
    }
 }

我对此比较陌生。

这是

你想要的吗?

$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?

最后,$result将包含 == 'yes' 的$inputArray中的第一项,或者将包含$inputArray中的第一项。

最有可能有更聪明的解决方案,但是......

<?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);

尝试类似

<?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]];
?>

php 函数 array_search . . . http://php.net/manual/en/function.array-search.php

希望这有帮助。

            <?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; 

无需经历如此漫长的过程,最好的解决方案可能是

if (array_search('yes', array_column($arrayvar, 'display'))) {
   Do whatever need;
}

你想要这样的东西:

$resultArray = [];
foreach($myArray as $array) {
    if($array['display?'] === 'yes') {
       $resultArray[] = $array;
       break;
    } 
}
if(empty($resultArray) {     
  $resultArray[] = $myArray[0]; 
}