0

I'm working in a code that find the next array value based on the current value but still always returning 1 as result.

$users_emails = array('Spence', 'Matt', 'Marc', 'Adam', 'Paul');

$current = 'Spence';
$keys = array_keys($users_emails);
$ordinal = (array_search($current,$keys)+1);
$next = $keys[$ordinal];
echo $next;

What's wrong?

chris85
  • 23,846
  • 7
  • 34
  • 51
John Wiky
  • 19
  • 5

5 Answers5

3

You are searching the wrong array to start with and echoing the wrong array too.

$users_emails = array('Spence', 'Matt', 'Marc', 'Adam', 'Paul');

$current = 'Spence';
$ordinal = (array_search($current,$users_emails)+1);
$next = $users_emails[$ordinal];
echo $next;

See my code, I search for Spence in the array with names and it returns a key number.
This key number should echo in user emails not keys.

https://3v4l.org/0gJ1m



If you need it to work with associative arrays you need to do like this:
$users_emails = array('a' => 'Spence', 'b' => 'Matt', 'c' => 'Marc', 'd' => 'Adam', 'e' => 'Paul');
$keys = array_values(array_keys($users_emails));

$current = 'Matt';
$next = ""; // set to not get notice if end of array

$ordinal = array_search($current,$users_emails);
$nextkey = (array_search($ordinal, $keys)+1);
If(!isset($keys[$nextkey])){
    // what to do if your at the end of array
    // $nextkey = 0;
    // Echo "message";
    // Or whatever you want
}else{
    $next = $users_emails[$keys[$nextkey]];
}
echo $next;

I use array_values on the keys to get a indexed array that accepts +1 to find the next key in the array.

https://3v4l.org/iVO6U

Andreas
  • 23,610
  • 6
  • 30
  • 62
2

$keys are the keys, not the value. Use the array with the $ordinal.

$next = $users_emails[$ordinal];

Demo: https://3v4l.org/REhGr

The array_keys gives you an array of the keys. Use your normal array for the array_search as well. Here's a visual of what you currently are building for $keys.

Array
(
    [0] => 0
    [1] => 1
    [2] => 2
    [3] => 3
    [4] => 4
)

Demo: https://3v4l.org/GQQcF

$users_emails = array('Spence', 'Matt', 'Marc', 'Adam', 'Paul');
$current = 'Marc';
$ordinal = (array_search($current, $users_emails)+1);
$next = !empty($users_emails[$ordinal]) ? $users_emails[$ordinal] : FALSE;
echo $next;
chris85
  • 23,846
  • 7
  • 34
  • 51
  • The result is always _Matt_ now :'( – John Wiky Apr 13 '18 at 03:19
  • How do I verifiy if the next row exists to avoid undefined offset? – John Wiky Apr 13 '18 at 03:24
  • @JohnWiky do you get this code to work? I sure can't with the provided links from Chris. – Andreas Apr 13 '18 at 03:43
  • @Andreas Why would this not work? The demos are for demonstration of the explanations. Here's a full link of the code my answer has in it https://3v4l.org/WZnD6 – chris85 Apr 13 '18 at 03:44
  • @chris the second link you got has "Marc" as subject and it returns "Matt" which is the **previous** not next item. – Andreas Apr 13 '18 at 03:46
  • @Andreas Guess I forgot to delete the below code. That link was just to show what the `array_keys` outputs. The actual code provided in my answer works. – chris85 Apr 13 '18 at 03:48
  • To avoid creating a new thread. If my array is like _array('a' => 'Spence', 'b' => 'Matt', 'c' => 'Marc', 'd' => 'Adam', 'e' => 'Paul');_. How can I search by these letters (a,b,c,d,e, ...) not from the names(Paul, Adam, Matt, etc)?? – John Wiky Apr 15 '18 at 02:40
  • You want to search by the key, not the value? Or you want to search by the value but then pull the next string key? – chris85 Apr 15 '18 at 03:08
2

Just use this one:

$users_emails = array('Spence', 'Matt', 'Marc', 'Adam', 'Paul');

$current = 'Spence';
$ordinal = array_search($current,$users_emails) + 1;
$next = $users_emails[$ordinal];
echo $next;
Phuc Lam
  • 370
  • 2
  • 8
1

Here's what I mean:

<?php
$users_emails = array('Spence', 'Matt', 'Marc', 'Adam', 'Paul');
$current = 'Spence'; $ordinal = array_search($current, $user_emails)+1;
$next = $user_emails[$ordinal];
echo $next;
?>

Depending what you're doing you may want to use next() instead:

<?php
$user_emails = array('Spence', 'Matt', 'Marc', 'Adam', 'Paul');
$current = current($user_emails); $next = next($user_emails); reset($user_emails);
echo $next;
?>
StackSlave
  • 10,613
  • 2
  • 18
  • 35
1

I have checked your code. In your code, array_keys function returns the indexes of $users_email as:

Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 )

Now you are searching $current = 'Spence'; in indexes array. That's why it returns 1.

You want the next value of searched string you should as:

$users_emails = array('Spence', 'Matt', 'Marc', 'Adam', 'Paul');

    $current = 'Spence';
    //$keys = array_keys($users_emails);//print_r($keys);
    $ordinal = (array_search($current,$users_emails)+1);
    $next = $users_emails[$ordinal];
    echo $next;

output:

Matt
Gufran Hasan
  • 8,910
  • 7
  • 38
  • 51