500, "gsda"=>1000, "bsdf"=>1500, "bads"=>2000, "iurt"=>2500, "poli"=>3000 ); How ...">

Next key in the 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 .

+9
source share
7 answers

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

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

+4
source

If the pointer is not on this element, as other solutions suggest, you can use

 <?php $keys = array_keys($arr); print $keys[array_search("gsda",$keys)+1]; 
+25
source
 $next = next($array); echo key($array); 

Must return the key corresponding to $ next;

+14
source
 next($array); $key = key($array); prev($array); 
+1
source

use a combo of the following and each to get both the key and the value of the next element:

 next($array) $keyvalueArray = each ( $array ) 

$ keyvalueArray should now hold the next key and value as "key" and "value"

http://www.php.net/manual/en/function.each.php

+1
source

you can use foreach

 $test=array( 1=> array("a","a","a"), 2=> array("b","b","b"), 'a'=> array("c","c","c") ); foreach (array_keys($test) as $value) { foreach ($test[$value] as $subValue) { echo $subValue." - ".$value; echo "\n"; } echo "\n"; } 

Exit

 a - 1 a - 1 a - 1 b - 2 b - 2 b - 2 c - a c - a c - a 
0
source
 function get_next_key_array($array,$key){ $keys = array_keys($array); $position = array_search($key, $keys); if (isset($keys[$position + 1])) { $nextKey = $keys[$position + 1]; } return $nextKey; 

// in the above function, the first argument is the array in which to search for the key, and the second argument is $ key, which is used to get the next key, so this means that you must get one existing key of the associative array from you, for which you want to get the following keys.

0
source

Source: https://habr.com/ru/post/897524/


All Articles