1.

How to get first element of array in php?

Answer»

There are various methods in PHP to get the first ELEMENT of an array. Some of the techniques are the use of reset function, array_slice function, array_reverse, array_values, foreach loop, etc.

Example

SUPPOSE we have an array like
$arrayVar = array('best', 'interview', 'question', 'com');

1. With direct accessing the 0th index:
echo $arrayVar[0];

2. With the help of reset()
echo reset($arrayVar);

3. With the help of foreach loop
foreach($arrayVar as $val) {
    echo $val;
    break; // exit from loop
}



Discussion

No Comment Found