tags:

views:

100

answers:

9

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 get this? I tried array_search but it was no use

A: 

Probably the easiest way is to just use a for loop, and return the loop counter when you find the string you are looking for.

Justin Ethier
A: 

Try the array_keys PHP function.

$key_string1 = array_keys($list, 'string1');
Evernoob
+4  A: 

array_search is the way to do it.

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.

RaYell
This returns the key, not the index. In your example the key is conveniently the index of the value.
smink
@smink, the OP's array is a non-associative array, so it works fine.
Matthew Flaschen
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
Aakash Chakravarthy
A: 

array_search should work fine, just tested this and it returns the keys as expected:

$list = array('string1', 'string2', 'string3');
echo "Key = ".array_search('string1', $list);
echo " Key = ".array_search('string2', $list);
echo " Key = ".array_search('string3', $list);

Or for the index, you could use

$list = array('string1', 'string2', 'string3');
echo "Index = ".array_search('string1', array_merge($list));
echo " Index = ".array_search('string2', array_merge($list));
echo " Index = ".array_search('string3', array_merge($list));
Vex
+1  A: 

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
hey! this worked for me!
Harish Kurup
A: 

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
sorry, i didn't specify the type of array. The array i use is a multi-dimensional array please see my answer above
Aakash Chakravarthy
+2  A: 

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
"array_values() was no use".. should be "array_search() was no use"
Vex
@Vex: Yes, you're right, thanks. I've edited to correct that.
Bill Karwin
A: 

Sorry, the array i use is a "multi-dimensional" array of the form

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

array_search() does not work for multidimensional arrays (i tried for this) So i preferred using foreach and incrementation

my solved code is

function find_array_index($to_find_Key = '', $in_Array = ''){
    $index = 0;
    foreach ($in_Array as $key => $value) {
        if($key == $to_find_Key){
            return $index;
        }
        $index++;
    }
}

using find_array_index('string2', $array); gave me the result 1

Anyway thanks all for your support !

Aakash Chakravarthy
Would've helped if you had mentioned the multi-dimensional array in the first place..
Vex
-1 This answer has nothing to do with the original question!
Bill Karwin
A: 

This code should do the same as your new routine, working with the correct multi-dimensional array..

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

 echo 'Index of "string2" = '. array_search('string2', array_keys($arr));
Vex