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.
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.
This returns the key, not the index. In your example the key is conveniently the index of the value. – smink Jun 2 '10 at 15:47 @smink, the OP's array is a non-associative array, so it works fine.
– Matthew Flaschen Jun 2 '10 at 15:48 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 Jun 3 '10 at 4:38.
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?
Array_values() was no use".. should be "array_search() was no use" – Vex Jun 2 '10 at 16:03 @Vex: Yes, you're right, thanks. I've edited to correct that. – Bill Karwin Jun 2 '10 at 16:21.
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.
This worked for me! – Harish Kurup Aug 21 '10 at 6:01.
Try the array_keys PHP function. $key_string1 = array_keys($list, 'string1').
$key = array_search('string2',$list) works fine for me. Are you trying to accomplish something more complex?
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 Jun 3 '10 at 4:37.
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)).
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)).
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!
Would've helped if you had mentioned the multi-dimensional array in the first place.. – Vex Jun 3 '10 at 10:04 -1 This answer has nothing to do with the original question! – Bill Karwin Jun 3 '10 at 22:04.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.