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

时间:2022-12-09 20:53:36

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。

4 个解决方案

#1


24  

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_keys parameter 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";
}

#2


17  

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

#3


9  

array_shift()

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

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

#4


0  

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

#1


24  

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_keys parameter 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";
}

#2


17  

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

#3


9  

array_shift()

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

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

#4


0  

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