PHP uses an array to implement the queue (in fact, first-out how to implement)

Source: Internet
Author: User

the array handler function of PHP also enables the array to be queued, and the stack is "advanced and out."

In the stack, the last data pressed (into the stack) will be ejected first (out of the stack). And the queue is FIFO. It's like the bank's automatic arranging machine.



The array is treated as a stack in PHP. The main use of Array_push () and Array_pop () two system functions to complete. The stack is mainly using the Array_push () function to add one or more elements to the tail of the first parameter. It then returns the length of the new array. The scale is as follows:

In PHP, the array is considered as a queue and is mainly implemented with Array_push () and Array_shift ().


Copy the Code code such as the following:
<?php
$zhan =array ("WEB");//declaring an array as a queue
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 to use the Array_shift () function to eject the first element of the array and reduce the length of the array by 1, as shown in the following scale:
Copy the Code code such as the following:
<?php
$zhan =array ("Web", "www.chhua.com", "Web Development note", "PHP", "Site Building");//Declare an array as a stack
Array_shift ($zhan);//String out of the queue (array)
Print_r ($zhan);//printing array contents Array ([0] = web[1] = www.chhua.com[2] + WEB Development Note [3] = PHP)
?>

PHP uses an array to implement the queue (in fact, first-out how to implement)

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.