3

I want to get the next value in a PHP array for example:

$array = array('a', 'b', 'c', 'd', 'e', 'f');
$current_array_val = 'c';
//so I want to run a code to get the next value in the array and
$next_array_val = 'd';
//And also another code to get the previous value which will be
$prev_array_val = 'b';

Please how do I run my code to achieve this

1

6 Answers 6

13

http://php.net/manual/ro/function.array-search.php

$index = array_search($current_array_val, $array);
if($index !== false && $index > 0 ) $prev = $array[$index-1];
if($index !== false && $index < count($array)-1) $next = $array[$index+1];
Sign up to request clarification or add additional context in comments.

1 Comment

See my answer with the array_flip() method for if you are dealing with large arrays (array_search() can become comparatively very slow with large datasets).
5

use next() function:

In addition: use current() or prev()

$array = array('a', 'b', 'c', 'd', 'e', 'f');

$current= current($array); // 'a'
$nextVal = next($array); // 'b'
$nextVal = next($array); // 'c'

// ... 

Comments

2

Here are another solution,

$values = array_values($array);
$search=array_search($current_array_val,$values);
$next=$values[(1+$search)%count($values)];

Comments

0

Use array_search, and increment/decrement for next/prev item in array.

$array = array('a', 'b', 'c', 'd', 'e', 'f');
$current_array_val = array_search('c', $array);
//so I want to run a code to get the next value in the array and
$next_array_val = $array[$current_array_val+1];
//And also another code to get the previous value which will be
$prev_array_val = $array[$current_array_val-1];


echo $next_array_val; // print d
echo $prev_array_val; // print b

3 Comments

array_search is slower than doing an array_flip (which indexes the values) when it comes to dealing with large arrays.
okok, thanks for tips, i'm not a pro php, just tying to help some people and learn by reading other answer
No problem, brother! Nothing wrong with your method when it comes to small sample sets. :)
0
$array = array('a', 'b', 'c', 'd', 'e', 'f');

$flipped_array = array_flip($array);

$middle_letter = 'c'; //Select your middle letter here

$index_of_middle_letter = $flipped_array[$middle_letter];

$next_index = $index_of_middle_letter + 1;
$prev_index = $index_of_middle_letter - 1;
$next_item = $array[$next_index];
$prev_item = $array[$prev_index];

array_search() is slower than doing an array_flip() when it comes to dealing with large arrays. The method I have described above is far more scalable.

Comments

0

Take a look at this code sample for a better understanding of the object-oriented way of navigating through arrays:

$array = array('a', 'b', 'c', 'd', 'e', 'f');

$pointer = 'c';

// Create new iterator
$arrayobject = new ArrayObject($array);
$iterator = $arrayobject->getIterator();

$iterator->seek($pointer);     //set position

// Go to the next value
$iterator->next();             // move iterator 

// Assign next value to a variable
$nextValue = $iterator->current();

Comments

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.