How to Get First 10 Elements of Array in PHP?

By Hardik Savani May 14, 2024 Category : PHP

Hello Artisan,

This tutorial will provide an example of php array get first 10 elements. you will learn php array get 10 first element value. if you want to see an example of how to get 10 first element of array in php then you are in the right place. if you want to see an example of php get 10 first key of array example then you are in the right place.

we will use array_slice() function to get first 10 elements of array in php. so, let's see the simple code of how to get first 10 values in php array.

Example:

index.php

<?php

$myArray = ["One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve"];

$firstElems = array_slice($myArray, 0, 10);

var_dump($firstElems);

?>

Output:

array(10) {

[0]=> string(3) "One"

[1]=> string(3) "Two"

[2]=> string(5) "Three"

[3]=> string(4) "Four"

[4]=> string(4) "Five"

[5]=> string(3) "Six"

[6]=> string(5) "Seven"

[7]=> string(5) "Eight"

[8]=> string(4) "Nine"

[9]=> string(3) "Ten"

}

I hope it can help you...

Tags :
Shares