How to Get first N number of Elements From an Array in PHP?
Last Updated :
31 Jul, 2024
Given an array, the task is to get the first N number of elements from the array in PHP. There are various approaches to achieve this task. In this article, we will explore all approaches with detailed explanations.
These are the following approaches:
Using array_slice() Function
The array_slice() function is a basic method to get the first N elements from an array. It extracts a portion of an array and returns it.
Example: This example shows the checking of if an array is a subset of another array using array_diff() function.
PHP
<?php
$arr = [1, 2, 3, 4, 5, 6];
$N = 3;
$firstNElements = array_slice($arr, 0, $N);
print_r($firstNElements);
?>
OutputArray
(
[0] => 1
[1] => 2
[2] => 3
)
Using for Loop
Using a for loop provides a more manual approach to extract the first N elements from an array.
Example: This example shows the checking of if an array is a subset of another array using for loop.
PHP
<?php
$arr = [1, 2, 3, 4, 5, 6];
$N = 3;
$firstNElements = [];
for ($i = 0; $i < $N && $i < count($arr); $i++) {
$firstNElements[] = $arr[$i];
}
print_r($firstNElements);
?>
OutputArray
(
[0] => 1
[1] => 2
[2] => 3
)
Using foreach Loop
The foreach loop offers another way to achieve this, especially when you want to make the code more readable.
Example: This example shows the checking of if an array is a subset of another array using foreach loop.
PHP
<?php
$arr = [1, 2, 3, 4, 5, 6];
$N = 3;
$firstNElements = [];
$count = 0;
foreach ($arr as $value) {
if ($count++ < $N) {
$firstNElements[] = $value;
} else {
break;
}
}
print_r($firstNElements);
?>
OutputArray
(
[0] => 1
[1] => 2
[2] => 3
)
Using array_splice() Function
The array_splice() function can also be used to remove elements from an array and return them. This method modifies the original array.
Example: This example shows the checking of if an array is a subset of another array using array_splice() Function.
PHP
<?php
$arr = [1, 2, 3, 4, 5, 6];
$N = 3;
$firstNElements = array_splice($arr, 0, $N);
print_r($firstNElements);
?>
OutputArray
(
[0] => 1
[1] => 2
[2] => 3
)
Using array_filter() Function
The array_filter() function can be used with a callback to create a new array containing only the first N elements.
Example:
PHP
<?php
$arr = [1, 2, 3, 4, 5, 6];
$N = 3;
$count = 0;
$firstNElements = array_filter($arr, function() use (&$count, $N) {
return $count++ < $N;
});
print_r($firstNElements);
?>
OutputArray
(
[0] => 1
[1] => 2
[2] => 3
)
Each of these methods provides a way to extract the first N elements from an array, allowing you to choose the one that best fits your needs and coding style.
Similar Reads
JavaScript - How to Get First N Elements from an Array? There are different ways to get the first N elements from array in JavaScript.Examples:Input:arr = [1, 2, 3, 4, 5, 6], n = 3 Output: [1, 2, 3] Input:arr = [6, 1, 4, 9, 3, 5, 7], n = 4Output: [6, 1, 4, 9]1. Using slice() MethodThe slice() method is used to extract a part of an array and returns a new
4 min read
How to get total number of elements used in array in PHP ? In this article, we will discuss how to get total number of elements in PHP from an array. We can get total number of elements in an array by using count() and sizeof() functions. Using count() Function: The count() function is used to get the total number of elements in an array. Syntax: count(arra
2 min read
Remove First Element from an Array in PHP Given an array, the task is to remove the first element from an array in PHP. Examples:Input: arr = [1, 2, 3, 4, 5, 6, 7]; Output: 2, 3, 4, 5, 6, 7 Input: arr = [3, 4, 5, 6, 7, 1, 2] Output: 4, 5, 6, 7, 1, 2Below are the methods to remove the first element from an array in PHP:Table of ContentUsing
3 min read
How to get elements in reverse order of an array in PHP ? An array is a collection of elements stored together. Every element in an array belong to a similar data type. The elements in the array are recognized by their index values. The elements can be subjected to a variety of operations, including reversal. There are various ways to reverse the elements
4 min read
Find the Most Frequent Element in an Array in PHP Given an array, i.e. $arr, the task is to find the most frequent element in an array. There are multiple ways to solve this problem in PHP we will be going to discuss them. These problems can be used during data analysis, Web Analytics, or highly used in fraud detection. Example: Input: $array = [1,
2 min read