Using array_slice() to Extract Portions of an Array in PHP


Extract a portion of an array without modifying the original array using array_slice().

Source Code

$fruits = ['apple', 'banana', 'cherry', 'date', 'fig'];
$sliced = array_slice($fruits, 1, 3); // Starts at index 1 and extracts 3 elements
print_r($sliced); // Outputs: Array ( [0] => banana [1] => cherry [2] => date )
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments