php 如何在PHP中获取子数组?

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

How to get sub array in PHP?

phparrays

提问by wamp

I want to get an array composed of the first 4rows of the original array $arr,

我想得到一个由4原始数组的第一行组成的数组$arr

how to do it in PHP?

如何在 PHP 中做到这一点?

回答by Mark Baker

array_slice()

array_slice()

$subArray = array_slice($arr,0,4);

回答by Andy E

You need to use array_slice().

您需要使用array_slice().

$output = array_slice($arr, 0, 4);

回答by Vimard

Check this out.. this should help

看看这个..这应该有帮助

<?php
$input = array("a", "b", "c", "d", "e");

$output = array_slice($input, 2);      // returns "c", "d", and "e"
$output = array_slice($input, -2, 1);  // returns "d"
$output = array_slice($input, 0, 3);   // returns "a", "b", and "c"

// note the differences in the array keys
print_r(array_slice($input, 2, -1));
print_r(array_slice($input, 2, -1, true));
?>

From http://docs.php.net/array_slice.

来自http://docs.php.net/array_slice