How to Get First 2 Elements of Array in PHP?
Hello Friends,
This is a short guide on php array get first 2 elements. you will learn php array get 2 first element value. This post will give you a simple example of how to get 2 first element of array in php. This post will give you a simple example of php get 2 first key of array example.
we will use array_slice() function to get first 2 elements of array in php. so, let's see the simple code of how to get first 2 values in php array.
Example:
index.php
<?php
$myArray = ["One", "Two", "Three", "Four"];
$firstElems = array_slice($myArray, 0, 2);
var_dump($firstElems);
?>
Output:
array(2) {
[0]=> string(3) "One"
[1]=> string(3) "Two"
}
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Get Last 5 Elements of Array in PHP?
- How to Get Last 3 Elements of Array in PHP?
- How to Get Last 2 Elements of Array in PHP?
- How to Get Random Value from an Array in PHP?
- How to Get Last Element of Array in PHP?
- How to Use ChatGPT API in PHP?
- How to Get First Element of Array in PHP?
- How to Generate Random Alphanumeric String in PHP?
- How to Get Maximum Key Value of Array in PHP?
- How to Remove Empty Values from Array in PHP?
- How to Add Prefix in Each Key of PHP Array?
- How to Get Minimum Key Value of Array in PHP?
- How to Remove Null Values from Array in PHP?