PHP code for using arrays to implement the stack data structure _ PHP Tutorial

Source: Internet
Author: User
PHP code that uses arrays to implement the stack data structure. In the stack, the final pushed data (into the stack) will be first popped up (out of the stack ). That is, the data structure is "advanced and later" in data storage. In PHP, the array is regarded as a stack in the stack, and the final pushed data (into the stack) will be first popped up (out of the stack ).
That is, the data structure is "advanced and later" in data storage.
In PHP, the array is used as a stack, mainly using the array_push () and array_pop () system functions.
The array_push () function is used to add one or more elements to the end of the array of the first parameter, and then return the length of the new array. for example:

The code is as follows:


$ Zhan = array ("WEB"); // declare an array as a stack
Array_push ($ zhan, "PHP"); // Press the string into the stack (array ).
Array_push ($ zhan, "WWW. CHHUA. COM"); // Press an element.
Print_r ($ zhan); // Print the array content
?>


The output stack mainly uses the array_pop () function to bring up the last function of the array and reduce the length of the array by 1. the example is as follows:

The code is as follows:


$ Zhan = array ("WEB", "www.chhua.com", "WEB development notes", "PHP", "website construction"); // declare an array as a stack
Array_pop ($ zhan); // extract the string from the stack (array)
Print_r ($ zhan); // Print the Array content Array ([0] => WEB [1] => www.chhua.com [2] => WEB Development notes [3] => PHP)
?>

The producer (into the stack) will be first popped up (out of the stack ). That is, the data structure is "advanced and later" in data storage. In PHP, the array is treated as a stack...

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.