Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP数组值的下一个值_Php_Arrays - Fatal编程技术网

PHP数组值的下一个值

PHP数组值的下一个值,php,arrays,Php,Arrays,如何从数组中获取值的下一个值。 我有一个这样的数组 $items = array( '1' => 'two', '9' => 'four', '7' => 'three', '6'=>'seven', '11'=>'nine', '2'=>'five' ); 如何获得“四”或“九”的下一个值。如果是这种情况,您应该首先准备数组。根据给定的数组,索引似乎不是连续正确的。尝试使用array

如何从数组中获取值的下一个值。 我有一个这样的数组

 $items = array(
    '1'   => 'two',
    '9'   => 'four',
    '7' => 'three',
    '6'=>'seven',
    '11'=>'nine',
    '2'=>'five'        
);

如何获得“四”或“九”的下一个值。

如果是这种情况,您应该首先准备数组。根据给定的数组,索引似乎不是连续正确的。尝试使用array_values()函数

$items = array(
    '1'   => 'two',
    '9'   => 'four',
    '7' => 'three',
    '6'=>'seven',
    '11'=>'nine',
    '2'=>'five'        
);

$new_items = array_values($items);

$new_items = array(
    [0] => 'two',
    [1] => 'four',
    [2] => 'three',
    [3] => 'seven',
    [4] => 'nine',
    [5] =>'five'        
);
然后你就可以做这件事了

foreach($new_items as $key => $value) {
   // Do the code here using the $key
}

如果是这种情况,您应该首先准备阵列。根据给定的数组,索引似乎不是连续正确的。尝试使用array_values()函数

$items = array(
    '1'   => 'two',
    '9'   => 'four',
    '7' => 'three',
    '6'=>'seven',
    '11'=>'nine',
    '2'=>'five'        
);

$new_items = array_values($items);

$new_items = array(
    [0] => 'two',
    [1] => 'four',
    [2] => 'three',
    [3] => 'seven',
    [4] => 'nine',
    [5] =>'five'        
);
然后你就可以做这件事了

foreach($new_items as $key => $value) {
   // Do the code here using the $key
}
我有

$input=“九”;
$items=数组(
“1”=>“2”,
“9”=>“4”,
“7”=>“3”,
“6”=>“7”,
“11”=>“9”,
“2”=>“5”
);
$keys=数组_键($items);
$size=计数($key);
$foundKey=array\u search($input,$items);
如果($foundKey!==false)
{
$nextKey=array\u search($foundKey,$keys)+1;
echo“您的输入为:.$input.\n”;
echo“它的密钥是:“.$foundKey.\n”;
如果($nextKey<$size)
{
echo“下一个键=>的值是:“..keys[$nextKey]。”=>“$items[$keys[$nextKey]].”“\n”;
}
其他的
{
echo“在“..$foundKey之后没有更多的键;
}
}
这个想法是,因为键不是以任何真实的顺序排列,我需要通过获取所有键并将它们放入一个数组中,使它们的整数键是我们的顺序,来创建一个易于遍历的顺序。这样一来,
'1'
=0,
'9'
=1,
'11'
=4

然后我从那里找到与输入匹配的键。如果我找到它,我会得到该键和+1(下一个键)的位置。从那里,我可以使用输入+1位置的
$keys
中的字符串值引用
$items
中的数据

如果我们的输入是
'five'
,我们会遇到一个问题,因为
'five'
是数组中的最后一个值。最后一个if语句检查下一个键的索引是否小于键数,因为我们将拥有的最大索引是5,键数是6

虽然可以使用有序整数键将所有值放入数组中,但如果不使用,则会松开原始键。如果您先使用
array\u键
,那么实际上就没有必要使用
array\u值

$input=“九”;
$items=数组(
“1”=>“2”,
“9”=>“4”,
“7”=>“3”,
“6”=>“7”,
“11”=>“9”,
“2”=>“5”
);
$keys=数组_键($items);
$size=计数($key);
$foundKey=array\u search($input,$items);
如果($foundKey!==false)
{
$nextKey=array\u search($foundKey,$keys)+1;
echo“您的输入为:.$input.\n”;
echo“它的密钥是:“.$foundKey.\n”;
如果($nextKey<$size)
{
echo“下一个键=>的值是:“..keys[$nextKey]。”=>“$items[$keys[$nextKey]].”“\n”;
}
其他的
{
echo“在“..$foundKey之后没有更多的键;
}
}
这个想法是,因为键不是以任何真实的顺序排列,我需要通过获取所有键并将它们放入一个数组中,使它们的整数键是我们的顺序,来创建一个易于遍历的顺序。这样一来,
'1'
=0,
'9'
=1,
'11'
=4

然后我从那里找到与输入匹配的键。如果我找到它,我会得到该键和+1(下一个键)的位置。从那里,我可以使用输入+1位置的
$keys
中的字符串值引用
$items
中的数据

如果我们的输入是
'five'
,我们会遇到一个问题,因为
'five'
是数组中的最后一个值。最后一个if语句检查下一个键的索引是否小于键数,因为我们将拥有的最大索引是5,键数是6

虽然可以使用有序整数键将所有值放入数组中,但如果不使用,则会松开原始键。如果您先使用
array\u键
,那么实际上就没有必要使用
array\u值

希望这些帮助:

while (($next = next($items)) !== NULL) {   
    if ($next == 'three') {     
        break;      
    }
}
$next = next($items);
echo $next;
对于大型阵列,u可以使用:

$src = array_search('five',$items); // get array key
$src2 = array_search($src,array_keys($items)); // get index array (start from 0)
$key = array_keys($items); // get array by number, not associative anymore


// then what u need just insert index array+1 on array by number ($key[$src2+1])

echo $items[$key[$src2+1]];
希望这有助于:

while (($next = next($items)) !== NULL) {   
    if ($next == 'three') {     
        break;      
    }
}
$next = next($items);
echo $next;
对于大型阵列,u可以使用:

$src = array_search('five',$items); // get array key
$src2 = array_search($src,array_keys($items)); // get index array (start from 0)
$key = array_keys($items); // get array by number, not associative anymore


// then what u need just insert index array+1 on array by number ($key[$src2+1])

echo $items[$key[$src2+1]];

您可以在不首先使用数组_值的情况下执行foreach。这解决了什么问题?您可以在不使用数组_值的情况下执行foreach。这解决了什么问题?哈哈。但我很高兴它能帮助别人,但我认为如果搜索最后一个数组或搜索不在数组中,你需要完成我的答案。这将遇到非常大的数组的问题,比如说…1000个值。如果该值是第985个值,则必须等待循环的985次迭代才能得到答案。@Memor-X您是对的,我编辑了我的答案,谢谢您的intakelol。但我很高兴它能帮助别人,但我认为如果搜索最后一个数组或搜索不在数组中,你需要完成我的答案。这将遇到问题,比如说,在非常大的数组中有…1000个值。如果该值是第985个值,则必须等待循环的985次迭代才能得到答案。@Memor-X您是对的,我编辑了我的答案,谢谢您的介绍