获取数组中除了第一个元素之外的所有元素..?(PHP)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1919550/
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-25 04:18:35  来源:igfitidea点击:

Get all elements in array besides the first one.. ? (php)

phparrayscoding-style

提问by Roeland

Is there a way to specify getting all but the first element in an array? I generally use foreach() to loop through my arrays.

有没有办法指定获取数组中除第一个元素之外的所有元素?我通常使用 foreach() 来循环遍历我的数组。

say array(1,2,3,4,5), i would only want 2, 3, 4 ,5 to show and for it to skip 1.

说数组(1,2,3,4,5),我只想显示 2, 3, 4 ,5 并让它跳过 1。

回答by micahwittman

$arr = array(1,2,3,4,5);
$all_but_the_first_element_array = array_slice($arr, 1);

回答by Andrew Moore

There are multiple ways of approaching this problem.

有多种方法可以解决这个问题。

The first solution is to use a flag boolean to indicate the first element and proceed in your foreach

第一个解决方案是使用标志布尔值来指示第一个元素并在您的 foreach

$firstElement = true;

foreach($array as $key => $val) {
  if($firstElement) {
    $firstElement = false;
  } else {
    echo "$key => $val\n";
  }
}

If your elements are naturally numerically indexed, you do not need the boolean flag, you can simply check if the key is 0.

如果您的元素是自然数字索引的,则不需要布尔标志,您可以简单地检查键是否为 0。

foreach($array as $key => $val) {
  if($key === 0) continue;      

  echo "$key => $val\n";
}


The second way is to cheat your way into a naturally numerically indexed array if it isn't already. I will use array_keys()to get a naturally numerically indexed array of keys and loop it.

第二种方法是欺骗你进入一个自然数字索引的数组,如果它还没有的话。我将array_keys()用来获取一个自然数字索引的键数组并循环它。

$keys = array_keys($array);

foreach($keys as $index => $key) {
  if($index === 0) continue;   

  $val = $array[$key];
  echo "$key => $val\n";
}


The third way is to use the array internal pointer to skip the first element and then continue in a loop by using reset(), next(), list(), and each(). Performance and resource-wise, this is the best option. Maintainability suffers greatly though.

第三种方式是通过使用使用数组内部指针以跳过所述第一元件,然后继续在一个循环中reset()next()list(),和each()在性能和资源方面,这是最好的选择。但是可维护性受到很大影响。

reset($array); // Reset pointer to 0
next($array);  // Advance pointer to 1

while (list($key, $val) = each($array)) {
  echo "$key => $val\n";
}  


If you don't mind losing the first element of the array, you can array_shift()it.

如果您不介意丢失数组的第一个元素,则可以array_shift()

array_shift($array);

foreach($array as $key => $val) {
  echo "$key => $val\n";
}


You can also array_slice()the array. I'm also using count()in order to be able to set the preserve_keysparameter to true.

你也可以array_slice()数组。我也在使用count()以便能够将preserve_keys参数设置为true.

$sliced = array_slice($array, 1, count($array)-1, true);

foreach($sliced as $key => $val) {
  echo "$key => $val\n";
}

回答by santosc

array_shift()

数组移位()

http://us2.php.net/manual/en/function.array-shift.php

http://us2.php.net/manual/en/function.array-shift.php

example used in the site:

网站中使用的示例:

<?php
$stack = array("orange", "banana", "apple", "raspberry");
$fruit = array_shift($stack);
print_r($stack);
?> 

The above example will output:

上面的例子将输出:

Array
(
    [0] => banana
    [1] => apple
    [2] => raspberry
)

**remember that the pointer to the array is reset (new value) after the shift

**记住在移位后指向数组的指针被重置(新值)

回答by Sarfraz

Well, there can be many ways for that as we have great deal of array-manipulation functions available. However i use the following method for that:

好吧,因为我们有大量可用的数组操作函数,所以可以有很多方法。但是我使用以下方法:

$orig_array = array(1, 2, 3, 4 ,5);
array_shift($orig_array);
print_r($orig_array);