116

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?

Community
  • 1
  • 1
Aakash Chakravarthy
  • 10,523
  • 18
  • 61
  • 78
  • 1
    this is old .... but I wold like to know why: _"I tried array_search but it was no use"_ – dsdsdsdsd Feb 13 '14 at 08:22
  • If you have an associative array, as I did, this answer helps: https://stackoverflow.com/a/3365793/470749 – Ryan Jan 08 '20 at 15:32

7 Answers7

217

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.

ashleedawg
  • 20,365
  • 9
  • 72
  • 105
RaYell
  • 69,610
  • 20
  • 126
  • 152
23

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, "
ircmaxell
  • 163,128
  • 34
  • 264
  • 314
18

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));
Brian Berneker
  • 571
  • 6
  • 6
15

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?

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
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
Michael Hurley
  • 121
  • 1
  • 5
  • This answer has deviated too far from the asked question. The question is not talking about associative arrays. – mickmackusa Jan 17 '23 at 22:16
1

Try the array_keys PHP function.

$key_string1 = array_keys($list, 'string1');
Evernoob
  • 5,551
  • 8
  • 37
  • 49
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?

Jeff
  • 12,147
  • 10
  • 51
  • 87