PHP uses arrays to implement queues (actually, how to implement FIFO)

Source: Internet
Author: User
PHP uses arrays to implement queues (actually, how to implement first-in-first-out). PHP's array processing function can also implement arrays to implement queues. the stack is "first-in-first-out ". In the stack, the final pushed data (into the stack) will be first popped up (out of the stack ). The queue is the first-in-first-out mechanism, just like the bank's number generator.



In PHP, the array is used as a stack, mainly through two system functions: array_push () and array_pop. 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:

In PHP, when arrays are treated as queues, array_push () and array_shift () are used.

The code is as follows:


$ Zhan = array ("WEB"); // declare an array as a queue
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 uses the array_shift () function to bring up the first element 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_shift ($ zhan); // detaches a string from the queue (array)
Print_r ($ zhan); // Print the Array content Array ([0] => WEB [1] => www.chhua.com [2] => WEB Development notes [3] => PHP)
?>

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.