Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Next key in array

Tags:

arrays

php

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.

like image 560
Jordy Avatar asked Sep 17 '11 12:09

Jordy


People also ask

How do you find the next value in an array?

The next() function moves the internal pointer to, and outputs, the next element in the array. Related methods: prev() - moves the internal pointer to, and outputs, the previous element in the array. current() - returns the value of the current element in an array.

What is key in PHP array?

The key() function simply returns the key of the array element that's currently being pointed to by the internal pointer. It does not move the pointer in any way. If the internal pointer points beyond the end of the elements list or the array is empty, key() returns null .

What is key and value in array?

What are a key and value in an array? Keys are indexes and values are elements of an associative array. Associative arrays are basically objects in JavaScript where indexes are replaced by user-defined keys. They do not have a length property like a normal array and cannot be traversed using a normal for loop.

How get key from value in array in PHP?

If you have a value and want to find the key, use array_search() like this: $arr = array ('first' => 'a', 'second' => 'b', ); $key = array_search ('a', $arr); $key will now contain the key for value 'a' (that is, 'first' ).


1 Answers

If pointer is not on this element, as other solutions assume, You can use

<?php
    $keys = array_keys($arr);
    print $keys[array_search("gsda",$keys)+1];
like image 82
RiaD Avatar answered Oct 20 '22 13:10

RiaD