return first element of array php code example

Example 1: php get first character of string

$firstStringCharacter = substr("hello", 0, 1);

Example 2: first item in array php

$firstItem = array_shift($array);

Example 3: php get first element of array

$colors = array(2=>"blue",3 =>"green",1=>"red");
$firstValue = reset($colors); //blue
$firstKey = key($colors); //2

Example 4: php get first element of iterator class

$first = $iterator->current();

// remember to rewind it after a foreach
foreach($iterator as $it)
{ /* ... */ }
$iterator->rewind();
$first = $iterator->current();