1

I want to find the first occurrence of any of several values in an array.

$sentence = array(I, want, to, go, to, the, market);
if(in_array(array('swim','fly','go'), $sentence)) {
    // Should return KEY = 3 as 'go' was found in the third key of the array
    }

I'm sure this must be fairly common, how can it be done?

lisovaccaro
  • 32,502
  • 98
  • 258
  • 410
  • This does what you want http://stackoverflow.com/questions/11529280/how-to-find-index-of-object-in-php-array/11529377#11529377 – fdomig Jul 25 '12 at 05:48

5 Answers5

1

http://tr.php.net/manual/en/function.array-keys.php

I think more specifically you are looking for this type of functionality?

<?php
$array = array(0 => 100, "color" => "red");
print_r(array_keys($array));

$array = array("blue", "red", "green", "blue", "blue");
print_r(array_keys($array, "blue"));

$array = array("color" => array("blue", "red", "green"),
               "size"  => array("small", "medium", "large"));
print_r(array_keys($array));
?>

The above example will output:

Array
(
    [0] => 0
    [1] => color
)
Array
(
    [0] => 0
    [1] => 3
    [2] => 4
)
Array
(
    [0] => color
    [1] => size
)
bruchowski
  • 5,043
  • 7
  • 30
  • 46
0

Please use this function array_search it will return key of the element if element found in the array

Example

$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');

$key = array_search('green', $array); // $key = 2;
$key = array_search('red', $array);   // $key = 1;
Praveen kalal
  • 2,148
  • 4
  • 19
  • 33
  • array_search only searches for one value, I need to search for several values at once, I could use it with a loop but I was hoping there was already a php function that did the trick – lisovaccaro Jul 25 '12 at 05:51
0

first of all you should read manual properly of in_array function , you were passing parameters in wrong order

$sentence = array(I, want, to, go, to, the, market);

 $found_key = array();   
 foreach($sentence as $key => $word)
    {
      if(in_array($word,array('swim','fly','go')))  
       { $found_keys[] = $key;}
    }
   print_r($found_keys);

I hope this is what you need

Rupesh Patel
  • 3,015
  • 5
  • 28
  • 49
  • The problem with this would be that if more than 1 of the values are present it would return the first it finds on the loop, even when the other value might have a lower key. – lisovaccaro Jul 25 '12 at 05:50
  • but the question was about to find the first occurrence i guess. – Rupesh Patel Jul 25 '12 at 05:51
0

array_keys would work a treat.

$keys = array_keys($sentence,array('swim','fly','go');

Then $keys will be a list of keys where swim, fly and go appear in $sentence

Just as the code below will return a list of keys that have empty values

$array = array('one','','three');
$keys = array_keys($array,"");
Darian Brown
  • 158
  • 1
  • 7
0
function find_in_array($needles,$array) {
    $pos = false;
    foreach ($needles as $key => $value) {
        if (in_array($value, $array)) {
            if(!$pos || array_search($value, $array) < $pos) {
                $pos = array_search($value, $array);
                }
            }
        }
        return $pos;
    }

echo find_in_array($values,$sentence);

This is what I am using right now, I'm testing array_keys as suggested by Darian Brown

lisovaccaro
  • 32,502
  • 98
  • 258
  • 410