118

I have an array:

$list = array('string1', 'string2', 'string3');

I want to get the index for a given value (i.e. 1 for string2 and 2 for string3)

All I want is the position of the strings in the array

  • string1 is 0
  • string2 is 1
  • string3 is 2

How to achieve this?

2
  • 1
    this is old .... but I wold like to know why: "I tried array_search but it was no use"
    – dsdsdsdsd
    Feb 13, 2014 at 8:22
  • If you have an associative array, as I did, this answer helps: stackoverflow.com/a/3365793/470749
    – Ryan
    Jan 8, 2020 at 15:32

7 Answers 7

219

array_search is the way to do it.

array_search ( mixed $needle , array $haystack [, bool $strict = FALSE ] ) : mixed

From the docs:

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

$key = array_search('green', $array); // $key = 2;
$key = array_search('red', $array);   // $key = 1;

You could loop over the array manually and find the index but why do it when there's a function for that. This function always returns a key and it will work well with associative and normal arrays.

5
  • 28
    This returns the key, not the index. In your example the key is conveniently the index of the value. Jun 2, 2010 at 15:47
  • 1
    @smink, the OP's array is a non-associative array, so it works fine. Jun 2, 2010 at 15:48
  • 1
    sorry, i didn't specify the type of array. The array i use is a multi-dimensional array please see my answer for the solution Jun 3, 2010 at 4:38
  • 1
    I have this type of array : [ 0 => { id : 1, name : something}, 1 => {.....}, ...]. is this possible to find key/index like array_search("name"=>something,$array) ?
    – lazzy_ms
    Jun 14, 2018 at 6:18
  • How about if the array has child ? May 8, 2020 at 5:35
25

If you're only doing a few of them (and/or the array size is large), then you were on the right track with array_search:

$list = array('string1', 'string2', 'string3');
$k = array_search('string2', $list); //$k = 1;

If you want all (or a lot of them), a loop will prob do you better:

foreach ($list as $key => $value) {
    echo $value . " in " . $key . ", ";
}
// Prints "string1 in 0, string2 in 1, string3 in 2, "
0
20

The problem is that you don't have a numerical index on your array.
Using array_values() will create a zero indexed array that you can then search using array_search() bypassing the need to use a for loop.

$list = ['string1', 'string2', 'string3'];
$index = array_search('string2',array_values($list));
0
16

Other folks have suggested array_search() which gives the key of the array element where the value is found. You can ensure that the array keys are contiguous integers by using array_values():

$list = array(0=>'string1', 'foo'=>'string2', 42=>'string3');
$index = array_search('string2', array_values($list));
print "$index\n";

// result: 1

You said in your question that array_search() was no use. Can you explain why? What did you try and how did it not meet your needs?

0
10

// or considering your array structure:

$array = array(
  'string1' => array('a' => '', 'b' => '', 'c' => ''),
  'string2' => array('a' => '', 'b' => '', 'c' => ''),
  'string3' => array('a' => '', 'b' => '', 'c' => ''),
);

// you could just

function findIndexOfKey($key_to_index,$array){
  return array_search($key_to_index,array_keys($array));
}

// executed

print "\r\n//-- Method 1 --//\r\n";
print '#index of: string1 = '.findIndexofKey('string1',$array)."\r\n";
print '#index of: string2 = '.findIndexofKey('string2',$array)."\r\n";
print '#index of: string3 = '.findIndexofKey('string3',$array)."\r\n";

// alternatively

print "\r\n//-- Method 2 --//\r\n";
print '#index of: string1 = '.array_search('string1',array_keys($array))."\r\n";
print '#index of: string2 = '.array_search('string2',array_keys($array))."\r\n";
print '#index of: string3 = '.array_search('string3',array_keys($array))."\r\n";

// recursersively

print "\r\n//-- Method 3 --//\r\n";
foreach(array_keys($array) as $key => $value){
  print '#index of: '.$value.' = '.$key."\r\n";
}

// outputs

//-- Method 1 --//
#index of: string1 = 0
#index of: string2 = 1
#index of: string3 = 2

//-- Method 2 --//
#index of: string1 = 0
#index of: string2 = 1
#index of: string3 = 2

//-- Method 3 --//
#index of: string1 = 0
#index of: string2 = 1
#index of: string3 = 2
1
  • This answer has deviated too far from the asked question. The question is not talking about associative arrays. Jan 17, 2023 at 22:16
1

Try the array_keys PHP function.

$key_string1 = array_keys($list, 'string1');
2
  • aren't those parameters reversed?
    – ashleedawg
    Jan 28, 2019 at 10:28
  • @ashleedawg No you have misread, array_keys is different to array_search
    – A Friend
    Nov 20, 2019 at 19:43
1

Could you be a little more specific?

$key = array_search('string2',$list)

works fine for me. Are you trying to accomplish something more complex?

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.