Php is the easiest way to get the final element of the array-PHP source code

Source: Internet
Author: User
Ec (2); & lt ;? In the php tutorial, the simplest way is to get the final element value of the array & nbsp; the Code is as follows: copy the code method 1 $ urlwww.111cn.net. cc; $ tmpUrlexplode (., $ url); Output temurl to see print_r ($ tmpUrl); * result: & nbsp; the Code is copied as follows on behalf of script ec (2); script

// In the php tutorial, obtain the last element of the array.

The Code is as follows:
// Method 1
$ Url = 'www .111cn.net. CC ';
$ TmpUrl = explode ('.', $ url );

// Output the temurl.
Print_r ($ tmpUrl );
/*
Result:

The Code is as follows:
Array
(
[0] => www
[1] => 111cn
[2] => net
[3] => cc
)


*/

// Let's look at method 1.

The Code is as follows:

Echo end ($ tmpUrl); // cc

// Method 2 array Length
$ Count = count ($ tmpUrl );
Echo $ tmpUrl [$ count-1]; // cc

/*
Summary:
Method 1 uses the php internal function end, which is convenient and convenient,
Method 2: Use count to calculate the length of the array and then subtract one to obtain the corresponding value.
Original Site Article www.111cn.net
*/

?>

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.