简体   繁体   中英

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

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.

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

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

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; 

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

You want something like this :

$resultArray = [];

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

if(empty($resultArray) {     
  $resultArray[] = $myArray[0]; 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM