Home  >  Article  >  Backend Development  >  PHP uses arrays to implement queues_PHP tutorial

PHP uses arrays to implement queues_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:20:52831browse

In PHP, an array is treated as a stack, which is mainly accomplished by using the two system functions array_push() and array_pop(). Pushing onto the stack mainly uses the array_push() function to add one or more elements to the end of the array of the first parameter, and then returns the length of the new array. The example is as follows:
In PHP, treating the array as a queue mainly means Implemented using array_push() and array_shift().

Copy code The code is as follows:

$zhan=array("WEB");/ /Declare an array as a queue
array_push($zhan,"PHP");//Push the string into the stack (array)
array_push($zhan,"WWW.CHHUA.COM");// Push another element
print_r($zhan);//Print the array content
?>

Pop the stack mainly to use the array_shift() function to move the first element of the array Pop up and reduce the length of the array by 1. The example is as follows:
Copy the code 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) ;//Dequeue the 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)
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325036.htmlTechArticlePHP treats an array as a stack, mainly using the two system functions array_push() and array_pop(). . Pushing onto the stack mainly uses the array_push() function to add one to the end of the array of the first parameter...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn