Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get sub array in PHP?

Tags:

arrays

php

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

how to do it in PHP?

like image 837
wamp Avatar asked May 28 '10 09:05

wamp


People also ask

How to get part of array in PHP?

PHP: Extract a slice of the arrayThe array_slice() function is used to extract a slice of an array. The array_slice() function returns the sequence of elements from the array array as specified by the starting_position and slice_length parameters. Array name. Specifies the beginning position of the slice in the array.

What is array_ slice in PHP?

The array_slice() function returns selected parts of an array. Note: If the array have string keys, the returned array will always preserve the keys (See example 4).

What does Array_splice () function do give an example?

The array_splice() function removes selected elements from an array and replaces it with new elements. The function also returns an array with the removed elements. Tip: If the function does not remove any elements (length=0), the replaced array will be inserted from the position of the start parameter (See Example 2).

What is an sub array?

A subarray is commonly defined as a part or section of an array. An array is a set of variables that a programmer defines collectively. Instead of creating separate variables, the programmer can declare a single array with multiple values labeled.


1 Answers

array_slice()

$subArray = array_slice($arr,0,4); 
like image 145
Mark Baker Avatar answered Sep 28 '22 17:09

Mark Baker