2

I have an associative array like below with, the actual array is much larger. This is just part of it. I want to access only the elements that have "[equipmentType] => WARRANTY" in it. In this case the element number is [39] but that element number changes, it's not always [39].

I know I can access $arrayName["equipment"]["39"]["name"] for example but how do I access it when it's not [39]?

In this particular example, there are 44 elements as you can see with the [equipmentCount] value.

I apologize if I'm not explaining this well.

Array
(
    [equipment] => Array
    (

        [2] => Array
            (
                [id] => 20073207920
                [name] => Mobile Connectivity
                [equipmentType] => OTHER
                [availability] => STANDARD
                [attributes] => Array
                    (
                        [0] => Array
                            (
                                [name] => Bluetooth
                                [value] => Bluetooth
                            )

                    )

            )

        [39] => Array
            (
                [id] => 200732343
                [name] => Rust, 5 Years,  /U Miles
                [equipmentType] => WARRANTY
                [availability] => STANDARD
                [attributes] => Array
                    (
                        [0] => Array
                            (
                                [name] => Warranty End Date
                                [value] => 09-Sep-2099
                            )
                    )

            )

    )

    [equipmentCount] => 44
)

Thanks!

3
  • 2
    Use a foreach loop to loop over that particular subArray and then access the name index in each iteration. Commented Aug 9, 2016 at 22:53
  • Thanks for your suggestion but how do I do that when the key [39] can be different? Associate arrays give me a headache ;-) Commented Aug 9, 2016 at 23:06
  • 2
    Just do foreach($variableName as $key => $name){echo "$key => $name <br />";} and you will see what you iterate over. Then I'm sure you will get it. Commented Aug 9, 2016 at 23:09

1 Answer 1

1

If I understand right, maybe that's what you want:

array_filter($array['equipment'], function($equip){
        return array_search('WARRANTY', $equip);
})
Sign up to request clarification or add additional context in comments.

1 Comment

Thank You! I was looking at array_filter but wasn't quite sure how to use it. Now I can just loop through the sub array as needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.