Vahid Alimohamadi
Vahid Alimohamadi

Reputation: 5868

Is there a built-in function in php which can return first row of an array?

I need to access the first row of an array but not like this:

print_r($array[0]) ;

something like this and be built-in :

echo first($array) ;

is there anything like that in php?

Upvotes: 0

Views: 45

Answers (2)

Suvash sarker
Suvash sarker

Reputation: 3170

$first_value_of_array = reset($array); // First Element's Value
$first_key_of_array = key($array); // First Element's Key

Hope this helps. :)

Upvotes: 2

Sven
Sven

Reputation: 70893

reset($array) will move the internal array pointer to the first element and return that element as well.

Note that "the first element" not necessarily means the element with index 0. It means the first element that got added to the array:

$array[1] = "foo";
$array[0] = "bar";
$first = reset($array); // foo

You might want to sort the array before fetching the "first" element.

Upvotes: 2

Related Questions