简体   繁体   中英

get the next key in array in php foreach loop

I have a select box and wanting to get the key of the next value in array to go with the option here is my code

<select>

<?php foreach ($make as $key => $make):?>
      <option value="<?php echo next($key);//not correct ?> - <?php echo $key; ?>"> <?php echo $make; ?></option>
<?php endforeach; 

Here is the array

 Array
(
    [0] => Brand
    [1] => Alfa Romeo
    [123] => Alpina
    [142] => Aston Martin
    [152] => Audi
    [619] => Bentley
    [640] => BMW
    [1122] => Buick

)

This will work with an associative array as well as numerically indexed:

<?php foreach ($make as $key => $value):?>
    <?php next($make); ?>
    <option value="<?php echo key($make); ?> - <?php echo $key; ?>"> <?php echo $value; ?></option>
<?php endforeach; ?>

Note, it's confusing and probably a bad idea to use the same variable name for iterating as the array. so instead of foreach ($make as $key => $make) I did foreach ($make as $key => $value) here.

The code above simply advances the pointer on the array and then gets the key for your option value using key() . Since there is no next key on the final array element, the value will be empty.

You can resolve your issue with a lot of solution, but if you have an assoiatve array or none order array or not numirc, you can use this:

<?php
$array = ["a" => 1, "b" => 2, "c" => 3, 1 => "aa"];

next($array);
$key = key($array);
echo $key;
prev($array);
$key = key($array);
echo $key;

Else, if array is ordered and numeric you can use $key + 1 ;

Simple way to get key of next element is to use array_search of next element of array eg

<?php foreach ($makers as $key => $make):?>
  <option value="<?php echo array_search( next($makers), $makers );"> <?php echo $make; ?></option>
 <?php endforeach; 

Hope this helps.

try this function. it finds current key of given value in the array, and from that found key's position, you can get next/previous key with $increment Ex: when $increment=1 , it finds next key when $increment=2 , it finds 2 next key when $increment=-1 , it finds 1 previous key, and so on.

function sov_find_key($findvalue, $array, $increment) {
    reset($array);
    $key = array_search($findvalue, $array);
    
    if ($key === false || $key === 0){
        return false;
    }
    
    if ($increment === 0){
        return $key;
    }
    
    $isNegative = $increment < 0 ? true:false;
    $increment = abs($increment);
    
    while(key($array) !== $key) {
        next($array);
    }
    
    $x=0;
    while($x < $increment) {
        if( $isNegative ){
            prev($array);
        } else {
            next($array);
        }
        $x++;
    }
    
    return key($array);
} 

DEMO: https://3v4l.org/CSSmR

<select>

<?php 
foreach ($make as $key => $make):
?>
      <option value="<?php echo $key + 1; ?> - <?php echo $key; ?>"> 
<?php echo $make; 
?>
</option>
<?php 
endforeach; 

Just increment the $key by 1, and you will have the next array element if the keys are in order and are numeric.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM