Code for using arrays in PHP to implement the stack data structure

Source: Internet
Author: User

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 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: CopyCodeThe Code is as follows: <? PHP
$ 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:Copy codeThe Code is as follows: <? PHP
$ 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)
?>

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.