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
.
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.
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 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.
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' ).
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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With