Jordy Jordy - 2 months ago 16
PHP Question

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.

Thanks!

Answer

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().

Comments