0

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

0

3 Answers 3

3

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

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

Comments

1

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]];

3 Comments

lol. but im glad it help someone, but i think u need to complete my answer if search last array or search not in array.
this will run into problems with very larges arrays with say.....1000 values in them. if the value was the 985th value you would have to wait 985 iterations of the loop before you get an answer.
@Memor-X you are right, i edit my answer, thanks for your intake
0

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
}

1 Comment

you can do foreach without using array_values first. What does this resolve?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.