ItSolutionStuff.com

How to Get First 3 Elements of Array in PHP?

By Hardik Savani • May 14, 2024
PHP

Hi Artisan,

In this tutorial, we will go over the demonstration of php array get first 3 elements. We will look at an example of php array get 3 first element value. I’m going to show you about how to get 3 first element of array in php. you'll learn php get 3 first key of array example.

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

Example:

index.php

<?php

$myArray = ["One", "Two", "Three", "Four", "Five", "Six"];

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

var_dump($firstElems);

?>

Output:

array(3) {

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

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

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

}

I hope it can help you...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

šŸ“ŗ Subscribe on YouTube

We Are Recommending You

ā˜…

How to Get Last 10 Elements of Array in PHP?

Read Now →
ā˜…

How to Get Last 5 Elements of Array in PHP?

Read Now →
ā˜…

How to Get Last 3 Elements of Array in PHP?

Read Now →
ā˜…

How to Get Last 2 Elements of Array in PHP?

Read Now →
ā˜…

How to Get Random Value from an Array in PHP?

Read Now →
ā˜…

How to Get Last Element of Array in PHP?

Read Now →
ā˜…

How to Use ChatGPT API in PHP?

Read Now →
ā˜…

How to Get First Element of Array in PHP?

Read Now →
ā˜…

How to Remove Specific Word from String in PHP?

Read Now →
ā˜…

How to Get Maximum Key Value of Array in PHP?

Read Now →
ā˜…

How to Remove Empty Values from Array in PHP?

Read Now →
ā˜…

How to Add Prefix in Each Key of PHP Array?

Read Now →
ā˜…

How to Get Minimum Key Value of Array in PHP?

Read Now →
ā˜…

How to Remove Null Values from Array in PHP?

Read Now →