Skip to content
Advertisement

Next key in array

I have an array:

$array=array(
    "sdf"=>500,
    "gsda"=>1000,
    "bsdf"=>1500,
    "bads"=>2000,
    "iurt"=>2500,
    "poli"=>3000
);

How can I get the name of the next key? For example if the current array is gsda, I need bsdf.

Advertisement

Answer

Update: Note that this is not a valid answer, as it’s searching for values instead of keys.


If the pointer of current() is on the right key, @Thomas_Cantonnet is right and you want to use next(). If you did not iterate through the array via next(), you first have to go through the array to set the internal index pointer correctly:

$search = "bsdf";
while (($next = next($array)) !== NULL) {
    if ($next == $search) {
        break;
    }
}

Now $next points to your current search-index and you can iterate over the rest via next().

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement