How to get the first element of an array in PHP?

PHPServer Side ProgrammingProgramming

To get the first element of an array in PHP, the code is as follows −

Example

 Live Demo

<?php
   $arr = array( "p"=>"150", "q"=>"100", "r"=>"120", "s"=>"110", "t"=>"115", "u"=>"103", "v"=>"105", "w"=>"125" );
   echo "Array value ...\n";
   echo "Value 1 = " . $arr["p"], "\n";
?>

Output

This will produce the following output−

Array value ...
Value 1 = 150

Example

Let us now see another example −

 Live Demo

<?php
   $arr = array( "p"=>"150", "q"=>"100", "r"=>"120", "s"=>"110", "t"=>"115", "u"=>"103", "v"=>"105", "w"=>"125" );
   echo "Value 1 = " .reset($arr);
?>

Output

This will produce the following output −

Value 1 = 150
raja
Published on 24-Dec-2019 16:46:44
Advertisements