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

Source: Internet
Author: User
In the stack, the last data pressed (into the stack) will be ejected first (out of the stack).
That is, the "advanced out" data structure is used when the data is stored.
In PHP, the array is treated as a stack, mainly using Array_push () and Array_pop () two system functions.
The main use of the Array_push () function is to add one or more elements to the end of the first parameter's array, and then return the length of the new array, as shown in the following example:
Copy CodeThe 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 into an element again
Print_r ($zhan);//print array contents
?>

The stack is mainly using the Array_pop () function to eject the last function of the array and reduce the length of the array by 1, as shown in the following example:
Copy CodeThe code is as follows:
$zhan =array ("Web", "www.chhua.com", "Web Development note", "PHP", "website building");//Declare an array as a stack
Array_pop ($zhan);//String out of the stack (array)
Print_r ($zhan);//printing array contents Array ([0] = web[1] = www.chhua.com[2] + WEB Development Note [3] = PHP)
?>

http://www.bkjia.com/PHPjc/325033.html www.bkjia.com true http://www.bkjia.com/PHPjc/325033.html techarticle in the stack, the last data pressed (into the stack) will be ejected first (out of the stack). That is, the "advanced out" data structure is used when the data is stored. 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.