I want to get an array composed of the first 4
rows of the original array $arr
,
how to do it 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.
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).
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).
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.
array_slice()
$subArray = array_slice($arr,0,4);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With