php 从数组中返回前 x 项

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

Returning first x items from array

phparrays

提问by O..

I want to return first 5 items from array. How can I do this?

我想从数组中返回前 5 个项目。我怎样才能做到这一点?

回答by knittl

array_slicereturns a slice of an array

array_slice返回一个数组的切片

$sliced_array = array_slice($array, 0, 5)

is the code you want in your case to return the first five elements

是您希望在您的案例中返回前五个元素的代码

回答by Andrejs Cainikovs

array_splice— Remove a portion of the array and replace it with something else:

array_splice— 移除数组的一部分并将其替换为其他内容:

$input = array(1, 2, 3, 4, 5, 6);
array_splice($input, 5); // $input is now array(1, 2, 3, 4, 5)

From PHP manual:

来自 PHP 手册:

array array_splice ( array &$input , int $offset [, int $length = 0 [, mixed $replacement]])

If length is omitted, removes everything from offset to the end of the array. If length is specified and is positive, then that many elements will be removed. If length is specified and is negative then the end of the removed portion will be that many elements from the end of the array. Tip: to remove everything from offset to the end of the array when replacement is also specified, use count($input) for length .

如果省略 length ,则删除从 offset 到数组末尾的所有内容。如果指定了长度并且是正数,那么将删除许多元素。如果指定了 length 并且为负数,则删除部分的末尾将是数组末尾的许多元素。提示:要在还指定替换时删除从偏移量到数组末尾的所有内容,请使用 count($input) for length 。

回答by Anax

If you just want to output the first 5 elements, you should write something like:

如果你只想输出前 5 个元素,你应该这样写:

<?php

  if (!empty ( $an_array ) ) {

    $min = min ( count ( $an_array ), 5 );

    $i = 0;

    foreach ($value in $an_array) {

      echo $value;
      $i++;
      if ($i == $min) break;

    }

  }

?>

If you want to write a function which returns part of the array, you should use array_slice:

如果你想写一个返回数组一部分的函数,你应该使用array_slice:

<?php

  function GetElements( $an_array, $elements ) {
    return array_slice( $an_array, 0, $elements );
  }

?>

回答by Cesar

You can use array_slice function, but do you will use another values? or only the first 5? because if you will use only the first 5 you can use the LIMIT on SQL.

您可以使用 array_slice 函数,但是您会使用其他值吗?还是只有前5个?因为如果您只使用前 5 个,则可以在 SQL 上使用 LIMIT。

回答by Jason

A more object oriented way would be to provide a range to the #[] method. For instance:

一种更面向对象的方法是为 #[] 方法提供一个范围。例如:

Say you want the first 3 items from an array.

假设您想要数组中的前 3 个项目。

numbers = [1,2,3,4,5,6]

数字 = [1,2,3,4,5,6]

numbers[0..2] # => [1,2,3]

数字[0..2] # => [1,2,3]

Say you want the first x items from an array.

假设您想要数组中的前 x 项。

numbers[0..x-1]

数字[0..x-1]

The great thing about this method is if you ask for more items than the array has, it simply returns the entire array.

这种方法的好处在于,如果您要求的项目比数组多,它只会返回整个数组。

numbers[0..100] # => [1,2,3,4,5,6]

数字[0..100] # => [1,2,3,4,5,6]