php 如何跳过数组循环中的第一个键?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/371554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:31:36  来源:igfitidea点击:

How to skip the 1st key in an array loop?

phparrays

提问by aeran

I have the following code:

我有以下代码:

if ($_POST['submit'] == "Next") {
    foreach($_POST['info'] as $key => $value) {
        echo $value;
    }
}

How do I get the foreach function to start from the 2nd key in the array?

如何让 foreach 函数从数组中的第二个键开始?

回答by Michael Stum

For reasonably small arrays, use array_sliceto create a second one:

对于相当小的数组,使用array_slice创建第二个:

foreach(array_slice($_POST['info'],1) as $key=>$value)
{
    echo $value;
}

回答by Tom Haigh

foreach(array_slice($_POST['info'], 1) as $key=>$value) {
    echo $value;
}

Alternatively if you don't want to copy the array you could just do:

或者,如果您不想复制数组,您可以这样做:

$isFirst = true;
foreach($_POST['info'] as $key=>$value) {
    if ($isFirst) {
        $isFirst = false;
        continue;
    }   
    echo $value;
}

回答by Organic SEO Services

Couldn't you just unset the array...

你不能只是取消阵列...

So if I had an array where I didn't want the first instance, I could just:

因此,如果我有一个不需要第一个实例的数组,我可以:

unset($array[0]);

and that would remove the instance from the array.

这将从数组中删除实例。

回答by Sean McSomething

If you were working with a normal array, I'd say to use something like

如果您使用的是普通数组,我会说使用类似

foreach (array_slice($ome_array, 1) as $k => $v {...

foreach (array_slice($ome_array, 1) as $k => $v {...

but, since you're looking at a user request, you don't have any real guarantees on the order in which the arguments might be returned - some browser/proxy might change its behavior or you might simply decide to modify your form in the future. Either way, it's in your best interest to ignore the ordering of the array and treat POST values as an unordered hash map, leaving you with two options :

但是,由于您正在查看用户请求,因此您对返回参数的顺序没有任何真正的保证 - 某些浏览器/代理可能会改变其行为,或者您可能只是决定在未来。无论哪种方式,忽略数组的顺序并将 POST 值视为无序哈希映射符合您的最佳利益,为您留下两个选项:

  • copy the array and unsetthe key you want to ignore
  • loop through the whole array and continuewhen seeing the key you wish to ignore
  • 复制数组和unset要忽略的键
  • 遍历整个数组,并continue在看到要忽略的键时

回答by Irmantas

in loop:

在循环中:

if ($key == 0) //or whatever
   continue;

回答by zb'

Alternative way is to use array pointers:

另一种方法是使用数组指针:

reset($_POST['info']); //set pointer to zero
while ($value=next($_POST['info'])  //ponter+1, return value
{
  echo key($_POST['info']).":".$value."\n";
}

回答by staticsan

If you're willing to throw the first element away, you can use array_shift(). However, this is slow on a huge array. A faster operation would be

如果您愿意丢弃第一个元素,则可以使用array_shift(). 然而,这在一个巨大的阵列上很慢。更快的操作将是

reset($a);
unset(key($a));

回答by Dheeraj Verma

Working Code From My Website For Skipping The First Result and Then Continue.

从我的网站跳过第一个结果然后继续的工作代码。

<?php 

$counter = 0;

foreach ($categoriest as $category) { if ($counter++ == 0) continue; ?>

It is working on opencart also in tpl file do like this in case you need.

它也在 tpl 文件中使用 opencart,以防万一。

回答by alexn

On a array filled with 1000 elements the difference is quite minimal.

在填充了 1000 个元素的数组上,差异非常小。

Test:

测试:

<?php
function slice($a)
{
    foreach(array_slice($a, 1) as $key)
    {

    }

    return true;
}

function skip($a)
{
    $first = false;

    foreach($a as $key)
    {
        if($first)
        {
            $first = false;
            continue;
        }
    }

    return true;
}

$array = array_fill(0, 1000, 'test');

$t1 = time() + microtime(true);

for ($i = 0; $i < 1000; $i++)
{
    slice($array);
}

var_dump((time() + microtime(true)) - $t1);

echo '<hr />';

$t2 = time() + microtime(true);

for ($i = 0; $i < 1000; $i++)
{
    skip($array);
}

var_dump((time() + microtime(true)) - $t2);
?>

Output:

输出:

float(0.23605012893677)

浮动(0.23605012893677)

float(0.24102783203125)

浮动(0.24102783203125)

回答by alexn

if you structure your form differently

如果您以不同的方式构建表单

  <input type='text' name='quiz[first]' value=""/>
  <input type='text' name='quiz[second]' value=""/>

...then in your PHP

...然后在你的 PHP 中

if( isset($_POST['quiz']) AND 
    is_array($_POST['quiz'])) {

    //...and we'll skip $_POST['quiz']['first'] 
    foreach($_POST['quiz'] as $key => $val){
      if($key == "first") continue;
      print $val; 
    }
}

...you can now just loop over that particular structure and access rest normally

...您现在可以循环遍历该特定结构并正常访问 rest