How to Get First Element of array in PHP?

Hello Friends Today I will tell you through this tutorial how you can fetch its first element from any array through php. So let’s try to understand.

Get the first element of an array using php, PHP extract first array element, How To fetch the first element of an array by PHP

We are telling you 4 method of php to get the first element from an array. You have 4 method given below.

  1. array_slice() function
  2. Direct Accessing the 0th Index Value
  3. array_values() function
  4. Using foreach Loop Method

1.Direct Accessing the 0th Index Value:-

<?php 
$array = array('experts', 'php', 'web', 'tutorial', 'website'); 
echo $array[0] 
?>

2.Using array_slice() function:-

<?php 
$array = array( 
30 => 'experts', 
31 => 'php', 
32 => 'web',
33 => 'tutorial',
34 => 'website'
); 
echo array_slice($array, 0, 1)[0]; 
?>

3.Using array_values() function:-

<?php 
$array = array( 
30 => 'experts', 
31 => 'php', 
32 => 'web',
33 => 'tutorial',
34 => 'website'
); 
echo array_values($array)[0]; 
?>

4.Using foreach loop:-

<?php 
$array = array( 
30 => 'experts', 
31 => 'php', 
32 => 'web',
33 => 'tutorial',
34 => 'website'
); 
foreach($array as $name) { 
echo $name; 
// break loop after first iteration 
break; 
} 
?>