Jump to content

Get index of a key in an array


oskom

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.