1

I have the following simple array and php

$my_array=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"purple","e"=>"yellow");


$match_key = array_search("blue",$my_array);
                echo $match_key;

I would like to create two variables that are the array item either side of $match_key. So in the example above I am trying to end up with...

$previous = 'green';
$next = 'purple';

What is the best way to tackle this?

fightstarr20
  • 11,682
  • 40
  • 154
  • 278

2 Answers2

1

If you have no way of changing your array structure, you could do the following by "creating" a new array with just the values of the original array, via array_values()

$vals = array_values($my_array);
$match_key = array_search("blue", $vals);

echo "PREV: ". $vals [($match_key - 1)] . "\n";
echo "CURR: ". $vals[$match_key] ."\n";
echo "NEXT: ". $vals [($match_key + 1)] . "\n";

Which returns:

PREV: green
CURR: blue
NEXT: purple

Now you'll need to do key handling/etc to ensure the code doesn't throw any errors and handles your keys in a subtle way.


There are various other ways (inspired by this post) that harnesses in-built functions, although seemingly taking longer to process:

$match_key = array_search("blue", $my_array);
// loop through and set the array pointer to the $match_key element
while (key($my_array) !== $match_key) next($my_array);

$current = $my_array[$match_key];
$prev = prev($my_array);
next($my_array); // set the array pointer to the next elem (as we went back 1 via prev())
$next = next($my_array);

Which returns the previous, current & next as below:

CURRENT:  blue
PREVIOUS: green 
NEXT:     purple
Community
  • 1
  • 1
Darren
  • 13,050
  • 4
  • 41
  • 79
  • I'd suggest using min and max for limiting the next and previous keys, and array_values to ensure the array is always 0 indexed. – fubar Feb 22 '17 at 01:04
  • @Rob Agreed, although I've left the answer to the scope of the question. We'll see what he OP comes back with – Darren Feb 22 '17 at 01:06
0

Using array_values() is the way to go. This creates a new indexed array in the same order as your original array.

Then you can search that for your value using array_search() which returns the numeric index. Then just +1 for the next and -1 for the previous.

Of course you may want to verify that the value you are searching for actually exists. If so, then ensure index+1 and index-1 also exist and set as null if they don't.

Something like this:

$my_val = 'blue';

$int_indexes = array_values($my_array); // store all values into integer indexed array

if ($index = array_search($my_val, $int_indexes)) { // don't set prev-next if value not found
    $prev = array_key_exists($index - 1, $int_indexes) ? $int_indexes[$index - 1] : null;
    $next = array_key_exists($index + 1, $int_indexes) ? $int_indexes[$index + 1] : null;
}

echo "previous: $prev" . '<br>';
echo "this: $my_val" . '<br>';
echo "next: $next";

Gives you results:

//    previous:  green
//    this:      blue
//    next:      purple

And if your searched value is at the beginning or end, no worries, you just get a null value. And it your searched value isn't found, no worries, you just get 2 null values.

BizzyBob
  • 12,309
  • 4
  • 27
  • 51