Zach
Zach

Reputation: 1185

Get array part by multi-dimensional array value

I have the following array:

Array
(
    [fall] => Array
        (
            [info] => Array
                (
                    [Name] => Test
                    [Description] => Test description
                    [Slug] => tester
                )

            [images] => Array
                (
                    [0] => fall_1.jpg
                    [1] => fall_2.jpg
                    [2] => fall_3.jpg
                    [3] => fall_4.jpg
                )

        )

    [spring] => Array
        (
            [images] => Array
                (
                    [0] => spring_1.jpg
                    [1] => spring_2.jpg
                    [2] => spring_3.jpg
                    [3] => spring_4.jpg
                    [4] => spring_5.jpg
                )

        )

)

What I'm looking to do is get the fall array if both info exists and Slug is equal to tester. I researched and saw this question/answer but mine is dependent on a sub-array being available -- would it be the same idea?

As an example, if tester was the only param given, I'd want the fall array to be returned.

Upvotes: 0

Views: 84

Answers (2)

Shylo Hana
Shylo Hana

Reputation: 1870

Same answer as the one you linked.

if(is_array($your_array_name['fall']['info']) && $your_array_name['fall']['info']['Slug'] == 'tester') {
  // Execute code here...
}

Upvotes: 0

Mike
Mike

Reputation: 24383

You could easily just do

if (isset($array['fall']['info']['Slug']) && $array['fall']['info']['Slug'] == 'tester') {
    return $array['fall'];
}

Upvotes: 2

Related Questions