简体   繁体   中英

PHP array next value of a value

How to get next value of a value from array. I have one array like this

 $items = array(
    '1'   => 'two',
    '9'   => 'four',
    '7' => 'three',
    '6'=>'seven',
    '11'=>'nine',
    '2'=>'five'        
);

how to get next value of 'four' or 'nine'.

I have this

$input = "nine";

$items = array(
    '1'   => 'two',
    '9'   => 'four',
    '7' => 'three',
    '6'=>'seven',
    '11'=>'nine',
    '2'=>'five'
);

$keys = array_keys($items);
$size = count($keys);

$foundKey = array_search($input,$items);

if($foundKey !== false)
{
    $nextKey = array_search($foundKey,$keys)+1;
        echo "your input is: ".$input."\n";
        echo "it's key is: ".$foundKey."\n";
    if($nextKey < $size)
    {
        echo "the next key => value is: ".$keys[$nextKey]." => ".$items[$keys[$nextKey]]."\n";
    }
    else
    {
        echo "there are no more keys after ".$foundKey;
    }
}

the idea is that because the keys are not in any real order i need to make an easy to traverse order by getting all the keys and putting them into an array so that their integer keys are our order. this way '1' = 0, '9' = 1, '11' = 4.

from there i then locate which key matches our input. if i find it i get the position of that key and + 1 (the next key). from there i can reference the data in $items using the string value in $keys at the position of our input +1.

if our input is 'five' we run into a problem as 'five' is the last value in our array. so the last if statement checks if the index for the next key is less than the number of keys since the largest index we'll have is 5 and the number of keys we have is 6.

while you could use array_values to get all the values into an array using ordered integer keys by doing this you loose your original keys unless you also used array_keys . if you use array_keys first then there's really no need to use array_values

hope this help:

while (($next = next($items)) !== NULL) {   
    if ($next == 'three') {     
        break;      
    }
}
$next = next($items);
echo $next;

for large array u can use :

$src = array_search('five',$items); // get array key
$src2 = array_search($src,array_keys($items)); // get index array (start from 0)
$key = array_keys($items); // get array by number, not associative anymore


// then what u need just insert index array+1 on array by number ($key[$src2+1])

echo $items[$key[$src2+1]];

If that's the case, you should first prepare your array. Based on your given array, it seems the index is not consecutively correct. Try using array_values() function.

$items = array(
    '1'   => 'two',
    '9'   => 'four',
    '7' => 'three',
    '6'=>'seven',
    '11'=>'nine',
    '2'=>'five'        
);

$new_items = array_values($items);

$new_items = array(
    [0] => 'two',
    [1] => 'four',
    [2] => 'three',
    [3] => 'seven',
    [4] => 'nine',
    [5] =>'five'        
);

Then you can do the foreach..

foreach($new_items as $key => $value) {
   // Do the code here using the $key
}

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