Jump to content

Retrieving the index of a value in a multi-dimensional array


SCook

Recommended Posts

Hi gang,

 

Quick question here:  I want to retrieve a specific value in a multi-dimensional array.  array_search does not seem to work.  The array will have two keys for each index val1 and val2

 

After sorting the array, I want to retrieve the poistion in the array of val1 based on a specific value.  Any help?  Thanks.

you could probably make something work using array_filter and a callback function, but i'm no good there. i would do it the dumb (= easy  ;D) way:

$searchElem = "the element you are looking for";
$location = 0;
$dim1Count = count($array);
for($i = 0; $i < $dim1Count; ++$i){
    foreach($array[$i] as $bla){
          if ($bla == $searchElem){ //or strcmp() for strings or something
             break;
          }
    }
    $location = $i;
    break;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.