简体   繁体   English

用PHP查找下一个数组行

[英]Finding the next array row with PHP

I'm working in a code that find the next array value based on the current value but still always returning 1 as result. 我正在使用一个代码,该代码根据当前值查找下一个数组值,但仍然始终返回1作为结果。

$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? 怎么了?

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. 看到我的代码,我在数组中搜索名称为Spence的代码,它返回一个键号。
This key number should echo in user emails not keys. 此密钥号应在用户电子邮件中回显而不是密钥。

https://3v4l.org/0gJ1m 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. 我在键上使用array_values来获取一个接受+1索引的数组,以找到该数组中的下一个键。

https://3v4l.org/iVO6U https://3v4l.org/iVO6U

$keys are the keys , not the value . $keyskeys ,而不是value Use the array with the $ordinal . 将数组与$ordinal

$next = $users_emails[$ordinal];

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

The array_keys gives you an array of the key s. array_keys为您提供key s的数组。 Use your normal array for the array_search as well. 也将常规数组用于array_search Here's a visual of what you currently are building for $keys . 这是您当前为$keys构建的图像。

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

Demo: https://3v4l.org/GQQcF 演示: 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;

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;

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: 根据您的操作,您可能需要使用next()

<?php
$user_emails = array('Spence', 'Matt', 'Marc', 'Adam', 'Paul');
$current = current($user_emails); $next = next($user_emails); reset($user_emails);
echo $next;
?>

I have checked your code. 我已经检查了您的代码。 In your code, array_keys function returns the indexes of $users_email as: 在您的代码中, array_keys函数返回$users_email的索引为:

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

Now you are searching $current = 'Spence'; 现在,您正在搜索$current = 'Spence'; in indexes array. 在索引数组中。 That's why it returns 1 . 这就是为什么它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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM