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