oskom Posted March 13, 2008 Share Posted March 13, 2008 Hello all, I've been combing the array pages of php.net with no answer to my question, so here it is... Is there a method for getting a numeric index of a text key in an array given a corresponding variable? Here's an example: $arr = array("first" => "apples", "second" => "oranges", "third" => "bananas"); $var = "second"; //THIS IS A MADE-UP FUNCTION NAME, OBVIOUSLY... array_get_key_index($arr, $var); //THE DESIRED RESULT WOULD BE 2...OR MAYBE 1 IF YOU'RE ASSUMING STANDARD ARRAY INDEXING, BUT YOU GET THE IDEA. Is there a php function that gives this kind of result? Link to comment https://forums.phpfreaks.com/topic/96019-get-index-of-a-key-in-an-array/ Share on other sites More sharing options...
paul2463 Posted March 13, 2008 Share Posted March 13, 2008 $key = array_search('oranges', array_values($arr)); echo $key; you can look up in the <a href="http://uk2.php.net/manual/en/function.array-values.php"> MANUAL </a> for what the functions are doing Link to comment https://forums.phpfreaks.com/topic/96019-get-index-of-a-key-in-an-array/#findComment-491564 Share on other sites More sharing options...
sasa Posted March 13, 2008 Share Posted March 13, 2008 try <?php $arr = array("first" => "apples", "second" => "oranges", "third" => "bananas"); $var = "first"; //THIS IS A MADE-UP FUNCTION NAME, OBVIOUSLY... if(array_get_key_index($arr, $var) !== false) echo array_get_key_index($arr, $var); else echo 'err'; function array_get_key_index($arr, $var){// first index is 0 if (!is_array($arr)) return false; $a = array_keys($arr); return array_search($var, $a); } ?> Link to comment https://forums.phpfreaks.com/topic/96019-get-index-of-a-key-in-an-array/#findComment-491569 Share on other sites More sharing options...
oskom Posted March 13, 2008 Author Share Posted March 13, 2008 Well, that's not what I'm looking for. I'm searching for the order of the KEY, not the value. In other words, if I've got a variable named "pickles" and an array where the key of the 3rd item in the array is "pickles", I want the function to compare the variable and the array and tell me that my variable corresponds with the key of item number 3. It should assume that I don't know the value of that array item. Does that make sense? sasa, I may have posted this after your post. I'll test your function and see if it's the winner. Link to comment https://forums.phpfreaks.com/topic/96019-get-index-of-a-key-in-an-array/#findComment-491573 Share on other sites More sharing options...
paul2463 Posted March 13, 2008 Share Posted March 13, 2008 apologies for my first effort another way is $array = array("first" => "apples", "second" => "oranges", "third" => "bananas"); function findkeypos($arr, $var) { $array = (array_keys($arr)); $key = array_search($var, array_values($array)); return $key; } echo findkeypos($array, 'second'); //prints out 1 Link to comment https://forums.phpfreaks.com/topic/96019-get-index-of-a-key-in-an-array/#findComment-491580 Share on other sites More sharing options...
oskom Posted March 13, 2008 Author Share Posted March 13, 2008 Thanks, sasa & paul2463. Both methods worked! I'll flip a coin to see who wins the giant stuffed teddy bear... (time passes) It was sasa!! *NOTICE: All claims that someone will win a teddy bear are completely false. Any promises of cuddly toys should be excused as a fabrication of a pathological mind. Link to comment https://forums.phpfreaks.com/topic/96019-get-index-of-a-key-in-an-array/#findComment-491581 Share on other sites More sharing options...
oskom Posted March 13, 2008 Author Share Posted March 13, 2008 sasa, paul2463...I figured out from sasa's function that you can boil the whole thing down. from this: if(array_get_key_index($arr, $var) !== false) echo array_get_key_index($arr, $var); else echo 'err'; function array_get_key_index($arr, $var){// first index is 0 if (!is_array($arr)) return false; $a = array_keys($arr); return array_search($var, $a); } to this: echo array_search($var, array_keys($arr)); Obviously, I took out the error checking, but for simplicity's sake, this actually works better for my purposes. Thanks, people. Link to comment https://forums.phpfreaks.com/topic/96019-get-index-of-a-key-in-an-array/#findComment-491602 Share on other sites More sharing options...
sasa Posted March 13, 2008 Share Posted March 13, 2008 coss, but did you understund logic if i type short way good luck with php Link to comment https://forums.phpfreaks.com/topic/96019-get-index-of-a-key-in-an-array/#findComment-491608 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.