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

Scott Quested

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.

IaMaCuP

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I loop through multidimensional array without using foreach

From Dev

How to loop through multidimensional array?

From Dev

How can I loop through this array and get first elements?

From Dev

How can I add a value to a multidimensional array?

From Dev

How can I loop through an array

From Dev

How do I loop through a multidimensional array to retrieve keys where the value is one?

From Dev

Loop through one array and find value in a subarray of a different multidimensional array

From Dev

How can i loop through 2 arrays at once and assign colours from array to each value of the other array

From Dev

C# How can I check if a multidimensional array has a value and it's index?

From Dev

C# How can I check if a multidimensional array has a value and it's index?

From Dev

How can I check for a value in a conditional array?

From Dev

loop through multidimensional array in php

From Dev

Foreach loop through multidimensional array

From Dev

js - loop through multidimensional array

From Dev

How to Display multidimensional Array?

From Dev

How can I use loop variable in array?

From Dev

how to check multidimensional array for duplicated value

From Dev

How to check if multidimensional array contains same value?

From Dev

Display multidimensional array in foreach loop

From Dev

How can I loop through an array object and check each element to see if one is defined?

From Dev

how can i create and pass value in multidimensional array in php?

From Dev

How to check if value exists in multidimensional array and return that array

From Dev

Loop through each key and value of php multidimensional array

From Dev

check if value exists in multidimensional array

From Dev

Check value in multidimensional associative array

From Dev

how to loop through array and display the button as required

From Dev

How to loop through an array and check for duplicates?

From Dev

How do I loop through this array and call value/s?

From Dev

How can I loop through nested json and get array unique value from field

Related Related

  1. 1

    How can I loop through multidimensional array without using foreach

  2. 2

    How to loop through multidimensional array?

  3. 3

    How can I loop through this array and get first elements?

  4. 4

    How can I add a value to a multidimensional array?

  5. 5

    How can I loop through an array

  6. 6

    How do I loop through a multidimensional array to retrieve keys where the value is one?

  7. 7

    Loop through one array and find value in a subarray of a different multidimensional array

  8. 8

    How can i loop through 2 arrays at once and assign colours from array to each value of the other array

  9. 9

    C# How can I check if a multidimensional array has a value and it's index?

  10. 10

    C# How can I check if a multidimensional array has a value and it's index?

  11. 11

    How can I check for a value in a conditional array?

  12. 12

    loop through multidimensional array in php

  13. 13

    Foreach loop through multidimensional array

  14. 14

    js - loop through multidimensional array

  15. 15

    How to Display multidimensional Array?

  16. 16

    How can I use loop variable in array?

  17. 17

    how to check multidimensional array for duplicated value

  18. 18

    How to check if multidimensional array contains same value?

  19. 19

    Display multidimensional array in foreach loop

  20. 20

    How can I loop through an array object and check each element to see if one is defined?

  21. 21

    how can i create and pass value in multidimensional array in php?

  22. 22

    How to check if value exists in multidimensional array and return that array

  23. 23

    Loop through each key and value of php multidimensional array

  24. 24

    check if value exists in multidimensional array

  25. 25

    Check value in multidimensional associative array

  26. 26

    how to loop through array and display the button as required

  27. 27

    How to loop through an array and check for duplicates?

  28. 28

    How do I loop through this array and call value/s?

  29. 29

    How can I loop through nested json and get array unique value from field

HotTag

Archive