tags:

views:

66

answers:

5

I have an array like

$myArray =array
(
"0"=>array("dogs",98),
"1"=>array("cats",56),
"2"=>array("buffaloes",78)
)

How can I get a key by providing a value?

e.g. if i search for "buffaloes" array_search may return "2".

Thanks

+2  A: 
function asearch($key, $myArray) {
  for ($i = 0; $i < sizeof($myArray); $i++) {
    if ($myArray[$i][0] == $key) {
      return $i;
    }
  }
  return -1; # no match
}

Though, you'd probably want to restructure your array to:

$myarray = array(
  'dogs' => 98,
  'cats' => 56,
  'buffaloes' => 78
);

And just do:

$myArray['buffaloes']; # 78
Tor Valamo
+2  A: 

The only way you can do it is to iterate over every item and preform a Linear Search

$i = -1;
foreach ($myArray as $key => $item){
    if ( $item[0] == 'buffaloes' ){
        $i = $key;
        break;
    }
}
//$i now holds the key, or -1 if it doesn't exist

As you can see, it is really really inefficient, as if your array has 20,000 items and 'buffaloes' is the last item, you have to make 20,000 comparisons.

In other words, you need to redesign your data structures so that you can look something up using the key, for example a better way may be to rearrange your array so that you have the string you are searching for as the key, for example:

$myArray['buffaloes'] = 76;

Which is much much faster, as it uses a better data structure so that it only has to at most n log n comparisons (where n is the number of items in the array). This is because an array is in fact an ordered map.

Another option, if you know the exact value of the value you are searching for is to use array_search

Yacoby
Yeah, you are right. :)
baltusaj
+1  A: 

You can loop over each elements of the array, testing if the first element of each entry is equal to "buffaloes".

For instance :

foreach ($myArray as $key => $value) {
    if ($value[0] == "buffaloes") {
        echo "The key is : $key";
    }
}

Will get you :

The key is : 2


Another idea (more funny ?), if you want to whole entry, might be to work with array_filter and a callback function that returns true for the "bufalloes" entry :

function my_func($val) {
    return $val[0] == "buffaloes";
}
$element = array_filter($myArray, 'my_func');
var_dump($element);

Will get you :

array
  2 => 
    array
      0 => string 'buffaloes' (length=9)
      1 => int 78

And

var_dump(key($element));

Gves you the 2 you wanted.

Pascal MARTIN
+2  A: 
$myArray =array
(
"0"=>array("dogs",98),
"1"=>array("cats",56),
"2"=>array("buffaloes",78)
);

function findInArray($term, $array) {
    foreach($array as $key => $val) {
        if(in_array($term, $val, true)) {
            return $key;
        }
    }
}

echo findInArray('buffaloes', $myArray); // 2
echo findInArray(78, $myArray); // 2
Gordon
Thanks Gordon but what if I replace 98, 56 and 78 by 0. In that case it fails as it returns zero always.
baltusaj
Check the manual for in_array. I've true as the third param to it now and then it should work as expected again.
Gordon
+1  A: 

I never heard of built in function. If you want something more general then above solutions you shold write your own function and use recursion. maybe array_walk_recursive would be helpful

Adrian Serafin