PHP array operations

Source: Internet
Author: User
PHP values are easy to use. Loop traversal and class pointer (personally called) can be used. However, if the last value of the array is also traversed, does it consume a lot of performance? Method 1:

 

Output

// Output 8

Method 2:

 

Output

// Output 8


Method 3:

 

Output

Array ( [0] => 8 )



The second method has a drawback. the Array_pop () function will "fetch" the last number of the original data, which is equivalent to cutting, the original data will no longer have the last value.

For example:

 

Output:

Array ( [0] => 1 [1] => 2 [2] => 4 [3] => 6 )



The above are all built-in functions in PHP. In fact, we can also get the last element in the PHP array through some of our own methods.

 

Of course, if the built-in method can be implemented, we should try to use the built-in function of PHP, because the built-in method is the most efficient in terms of efficiency and time, but in the development process, if we do not know whether this method exists or do not remember this method, we can also write the function method we want based on our own logic.

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.