Code _php techniques for using arrays to implement stack data structures in PHP

Source: Internet
Author: User
In the stack, the last pressed data (into the stack) will be first ejected (out of the stack).
That is, the "advanced out" data structure is used in the data storage.
In PHP, the array is treated as a stack, mainly by using Array_push () and Array_pop () two system functions.
The stack mainly uses the Array_push () function to add one or more elements to the tail of the first parameter, and then returns the length of the new array, as follows:
Copy Code code 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"), or press into an element
Print_r ($zhan);//print array contents
?>

The stack mainly uses the Array_pop () function to eject the last function of the array and subtract the length of the array by 1, as shown in the following example:
Copy Code code as follows:

<?php
$zhan =array ("Web", "www.chhua.com", "Web Development Notes", "PHP", "website building");/declare an array as a stack
Array_pop ($zhan);//send string out of stack (array)
Print_r ($zhan);//print 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.