Php three methods for getting the last array value of the array

Source: Internet
Author: User
During the php development process of www.itokit.com2012112974834.html, you may need to obtain the final health or of the array. Here, the it Power summarizes three methods, and explains how to use the condition constraints of the three methods in some situations. How does PHPCode copy content to the clipboard? Php $ arrayarr

Http://www.itokit.com/2012/1129/74834.html php development process, may often need to retrieve the array of the last key or. Here, the it Power summarizes three methods, and explains how to use the condition constraints of the three methods in some situations. How does PHP Code copy content to the clipboard? Php $ array = arr

Http://www.itokit.com/2012/1129/74834.html

During php development, it is often necessary to obtain the final key or value of the array for the retrieved array. Here, the it Power summarizes three methods, and explains how to use the condition constraints of the three methods in some situations.


Copy content from PHP Code to clipboard

  1. $ Array = array (1, 2, 3, 4, 5 );
  2. Echo $ array [count ($ array)-1]; // calculates the length of the array and obtains the last element of the array. If the last element in the array contains a non-numeric key name, the result may not match the expectation.
  3. // Applies to an array with the key name and number
  4. Echo'
    ';
  5. Echo end ($ array); // point the internal pointer of the array to the last unit, applicable to all Arrays
  6. Echo'
    ';
  7. Rsort ($ array); // sorts arrays in reverse order. If the array contains letters or Chinese characters, the results may not meet expectations and are most suitable for numeric arrays.
  8. Echo $ array [0];
  9. ?>

In general, php's built-in function end is the best method. You can test it.

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.