Get the value of the first element of an unknown character key an array group by using the PHP current () function

Source: Internet
Author: User

Get the value of the first element of an unknown character key an array group by using the PHP current () function

In development often encountered such a problem, get the value of the first element of the array, if it is a digital index that is OK, direct $array[0], if the key name is a string, you do not know this string? You can do it with the current () function.

Of course, you can use the Array_shift () function, but it will break the original array (that is, delete the first element in the array and return the value of the deleted element).

About the current () function:

Each array has an internal pointer to its " current " cell, which initially points to the first cell inserted into the array. Obtained with current ().

Similar functions:

End () Moves the internal pointer of the array to the last cell and returns its value.

Next () returns the value of the next cell pointed to by the array's internal pointer, or FALSE if there are no more cells.

Prev () returns the value of the previous cell pointed to by the array's internal pointer, or FALSE if there are no more cells.

Reset () returns the internal pointer of the array back to the first cell and returns the value of the first array cell, False if the array is empty.

<?php
$arr = Array ("A" = "PHP", "Java", "C");
Echo current ($arr); //php
Echo Next ($arr); Java
Echo prev ($arr); PHP points to the value of the previous cell, so it's PHP again.
Echo End ($arr); C
?>

Get the value of the first element of an unknown character key an array group by using the PHP current () function

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.