Tim Tim - 3 months ago 11
PHP Question

PHP associative array access

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!

Answer

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

array_filter($array['equipment'], function($equip){
        return array_search('WARRANTY', $equip);
})
Comments