2

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

)
6
  • Are the keys numeric and nicely ordered Commented Nov 27, 2018 at 16:51
  • 5
    Just out of interest, what do you want to happen on the last iteration of the loop, when the "next" key doesn't exist? Commented Nov 27, 2018 at 16:52
  • 1
    And WHY would the value attribute want to contain something you cannot easily use to address to the correct value Commented Nov 27, 2018 at 16:53
  • 3
    There's nothing wrong with your question but it really sounds like a case for re-thinking your array structure rather than trying to do what you described Commented Nov 27, 2018 at 16:53
  • Yes they are numerical index Commented Nov 27, 2018 at 16:53

5 Answers 5

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Nice solution, I see your solution now, I think this way is best answer...so that I will vote for you...
I'm not sure why the OP unaccepted my answer but thanks
2

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;

Comments

0

Simple way to get key of next element is to use array_search of next element of array e.g.

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

Hope this helps.

Comments

0

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

Comments

-1
<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.

3 Comments

Are you sure this code runs with foreach ($make as $key => $make):?
this will not work for associative array. It will also throw return a non-existent key on the final iteration since $key + 1 doesn't exist in the array at that point. It may be what the OP wanted but it doesn't "get the next key in array in php foreach loop"
The OP stated his keys were numeric, so it's answers his question, but thanks, good job.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.