Search for values in nested array

Posted by dardub on Stack Overflow See other posts from Stack Overflow or by dardub
Published on 2010-04-22T18:14:08Z Indexed on 2010/04/22 18:23 UTC
Read the original article Hit count: 168

Filed under:
|

I have an array as follows

array(2) {
  ["operator"] => array(2) {
    ["qty"] => int(2)
    ["id"] => int(251)
  }
  ["accessory209"] => array(2) {
    ["qty"] => int(1)
    ["id"] => int(209)
  }
  ["accessory211"] => array(2) {
    ["qty"] => int(1)
    ["id"] => int(211)
  }
}

I'm trying to find a way to verify an id value exists within the array and return bool. I'm trying to figure out a quick way that doesn't require creating a loop. Using the in_array function did not work, and I also read that it is quite slow.

In the php manual someone recommended using flip_array() and then isset(), but I can't get it to work for a 2-d array.

doing something like

if($array['accessory']['id'] == 211)

would also work for me, but I need to match all keys containing accessory -- not sure how to do that

Anyways, I'm spinning in circles, and could use some help. This seems like it should be easy. Thanks.

© Stack Overflow or respective owner

Related posts about php

Related posts about multidimensional-array