php - Search in a multidimensional array and get an index -


i have array that:

printr_($photos); ======  array (     [0] => array         (             [path] => site:photos/photo-1.jpg             [data] => array                 (                     [phototitle] => mega title                     [photodate] => 2015                     [flickrurl] => xxx                     [portrait] =>                      [slug] => mega-title                 )          )      [1] => array         (             [path] => site:photos/photo-2.jpg             [data] => array                 (                     [phototitle] => photo title                     [photodate] => 2001                     [flickrurl] => xxx                     [portrait] =>                      [slug] => photo-title                 )          )  ... 

i array index string (which slug browser current url).
tried solution got error (undefined index: data […] on line 95)

/* ** search in array */ function arraysearch($array, $field, $search){     foreach($array $key => $value){         if ($value[$field] === $search)             return $key;     }     return false; }  // line 95 $photo_index = $photos[arraysearch($photos["data"], "slug", "mega-title")]; 

this should work php >= 5.5:

$key = array_search('mega-title', array_column(array_column($photos, 'data'), $slug)); $photo_index = $photos[$key]; 

Popular posts from this blog