Get the index value of an array in PHP

php get array key by value multidimensional
php get value from associative array by key
php get array value without key
php search multidimensional array for value
get specific key value from array php
php search multidimensional array for multiple values
php search array of objects
php get all values from multidimensional array

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?

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.

key - Manual, key() returns the index element of the current array position. Parameters ¶. array. The array. Return Values ¶. The key()  An array can hold many values under a single name, and you can access the values by referring to an index number. Create an Array in PHP. Complete PHP Array

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, "

Get the index value of an array in PHP, Try the array_keys PHP function. Here is a function that will work for numeric or string indices. Pass the array as first parameter, then the index to the element that needs to be moved and finally set the direction to -1 to move the element up and to 1 to move it down. We will use array_values () function to get all the values of the array and range () function to create an array of elements which we want to use as new keys or new index of the array (reindexing). Then the array_combine () function will combine both the array as keys and values. echo "Index: " . $key .

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?

PHP array_search() Function, The array_search() function search an array for a value and returns the key. Answer: Use the Array Key or Index If you want to access an individual value form an indexed, associative or multidimensional array you can either do it through using the array index or key. Let's check out the following example to understand how it basically works:

// 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

PHP - Get key name of array value, How do you find the index of an array in Python? Despite PHP's amazing assortment of array functions and juggling maneuvers, I found myself needing a way to get the FULL array key mapping to a specific value. This function does that, and returns an array of the appropriate keys to get to said (first) value occurrence.

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 = array('string1', 'string2', 'string3');
$index = array_search('string2',array_values($list));

Searching an Array, Using array_keys() function: The array_keys() function is an inbuilt function in PHP which is used to return either all the keys of an array or the subset of the keys. Program 1: Program to get numeric index of associative array using array_keys() function. print_r( array_keys ( $assoc_array )); current() - returns the value of the current element in an array; next() - moves the internal pointer to, and outputs, the next element in the array; prev() - moves the internal pointer to, and outputs, the previous element in the array; reset() - moves the internal pointer to the first element of the array

Array find in Python - Python Tutorial, This inbuilt function of PHP is used to search for a particular value in an array, and if Return Value: As already mentioned, the function returns the key of the  An array index can be any string value, even a value that is also a value in the array. The value of array["foo"] is "bar". The value of array["bar"] is "foo" The following expressions are both true: $array["foo"] == "bar" $array["bar"] == "foo"

PHP, strict − Possible values are TRUE or FALSE. Search for identical elements in the array, set to TRUE. Return. The array_search() function returns  This stores element values in association with key values rather than in a strict linear index order. Multidimensional array − An array containing one or more arrays and values are accessed using multiple indices. NOTE − Built-in array functions is given in function reference PHP Array Functions. Numeric Array. These arrays can store

How to get numeric index of associative array in PHP , Answer: Use the Array Key or Index. If you want to access an individual value form an indexed, associative or multidimensional array you can either do it through  An array is created using an array() function in PHP. There are basically three types of arrays in PHP: Indexed or Numeric Arrays: An array with a numeric index where values are stored linearly. Associative Arrays: An array with a string index where instead of linear storage, each value can be assigned a specific key.

  • A tech writer friend of mine told me: "you can't get the right answers unless you ask the right questions."
  • this is old .... but I wold like to know why: "I tried array_search but it was no use"
  • Please, mark the higher rated answer as accepted.
  • If you have an associative array, as I did, this answer helps:
  • This returns the key, not the index. In your example the key is conveniently the index of the value.
  • @smink, the OP's array is a non-associative array, so it works fine.
  • 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
  • And the asker marks the answer with a score of 1 as accepted facepalm
  • 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) ?
  • the foreach is what I needed
  • "array_values() was no use".. should be "array_search() was no use"
  • The question mentions nothing about multi-dimensional arrays(comments by the OP in other answers suggests that this answer is what he was actually looking for, but it if you only read the original question and this answer, what you are saying makes no sense)
  • aren't those parameters reversed?
  • @ashleedawg No you have misread, array_keys is different to array_search