Solved

# Getting the next key in an array

Posted on 2005-05-16
Medium Priority
284 Views
I have an array where the keys are numeric but not sequential, so its:

23, 354, 545, 2333 etc

I now have the value for a key, say 354 and I want to get the next key, in this case 545.

What is the best way to do this? Do I need to loop through the array, match the key and then get the next match. Is there a more efficient method?
0
• 2
• 2
• 2
• +2

LVL 32

Expert Comment

ID: 14014596
Since you need just the key - value pair, foreach loop will serve good:

\$myKey = false;
foreach(\$my_array as \$key=>\$value)
{
if(\$myKey) {
echo "\$key : \$value";
\$myKey = false;
// break;
}
if(\$value == YOUR_VALUE) {
\$myKey = true;
}
}

If you want to get the key for a value, array_search ( http://www.php.net/array_search ) function is handy.
0

LVL 11

Expert Comment

ID: 14014744
0

Author Comment

ID: 14014789
ZhaawZ,

How do you move the internal pointer of an array to a specific key?
0

LVL 11

Accepted Solution

ZhaawZ earned 2000 total points
ID: 14014813
\$arr = array (
23 => '1st',
354 => '2nd',
545 => '3rd',
2333  => '4th'
);

reset(\$arr);
while ( key(\$arr) != 354 ) { next(\$arr); }
next(\$arr);
echo key(\$arr);
0

Author Comment

ID: 14014853
Is that any different/better than the foreach method?
0

LVL 32

Expert Comment

ID: 14014931
Yes, foreach creates a copy of the array and loops through that copy. It is NOT as fast as ZhaawZ method. So go with it.
0

LVL 10

Expert Comment

ID: 14015826
Hi

I would just use the basic array functions with a loop, but when working with big array(s) it not very good when the key you are matching is at the end of the array! With big arrays use a preg_ function with implode! This way you don't need to use a loop or do 100(s) or even thousands of if(s) or repeated array function calls in that loop!

example...

<?

\$arr = array ( 23 => '1st', 354 => '2nd', 545 => '3rd', 2333 => '4th' );

\$key = 545;

if ( isset ( \$arr[\$key] ) )
{
\$next = preg_replace ( "!^.*?(" . \$key . ")\|?(.*)\|?!", "\\2", implode ( '|', array_keys( \$arr ) ) );

if ( ! empty ( \$next ) )
{
echo 'Next key: ' . \$next . ' => ' . \$arr[\$next];
}
else
{
echo 'key: ' . \$key . ', is the last element in array (arr) no next key';
}
}
else
{
echo \$key . 'does not exit!';
}

?>

Suzanne
0

LVL 49

Expert Comment

ID: 14016028
@Suzanne:

Lets say we would have
array(1 => array("test" => 1, "test2"=>2), 2, 3, 4);

Your code would show us that "test2" is the next key following key "test", although array["test2"] does not work.

-r-
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.
###### Suggested Courses
Course of the Month16 days, 9 hours left to enroll