top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How can we get the first element of an array in PHP?

+1 vote
404 views
How can we get the first element of an array in PHP?
posted Jun 9, 2014 by Rahul Mahajan

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote

Try this code for find first element of array.

$array = array( 4 => 'apple', 7 => 'orange', 13 => 'mango' );
echo $array[key($array)];

// key($array) -> will return the first key (which is 4 in this example)

answer Jun 10, 2014 by Vrije Mani Upadhyay
0 votes

We can get the first element of an array by the function current();
<?php $arr = array('name'=>'angel','age'=>'23','city'=>'delhi','profession'=>'php developer'); $firstvalue = current($arr); echo"$firstvalue"; ?>

OUTPUT : angel

answer Jun 10, 2014 by Mohit Sharma
...