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?

1
  • Get rid of $keys and use $user_emails. Commented Apr 13, 2018 at 3:15

5 Answers 5

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

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

Comments

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;

8 Comments

The result is always Matt now :'(
How do I verifiy if the next row exists to avoid undefined offset?
@JohnWiky do you get this code to work? I sure can't with the provided links from Chris.
@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 3v4l.org/WZnD6
@chris the second link you got has "Marc" as subject and it returns "Matt" which is the previous not next item.
|
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;

Comments

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

Comments

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

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.