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.