this article illustrates the stack and queue functions of PHP based array implementation. I want to share with you the following details:

stack and queue are two implementations of data structure, and they are containers that use very extensive data storage. Following it, application individually the two container in PHP:

1, the container stack into the stack, finally will be the first out of the stack, the so-called "advanced" data structure.

2, in PHP, array is used as a stack, array_push () function can be used, or $array[]=$value operation is used to accomplish stack operation. array_pop () function is used to complete stack operation. The operation of

3, the stack into the stack corresponds to the data one by one into a barrel (assuming that the data and the barrel with surface area, appropriate is just to go across) the container, the result is that after the completion of all the data into the stack, the stack at the bottom of the advanced.


 <?php    $mypara = array("para1");    echo(array_push($mypara,"para2"));  //添加一个数据到mypara数组    print_r($mypara);    $mypara1=array("a"=>"para11","b"=>"para12");    echo array_push($mypara1,"para13","para14");     print_r($mypara1);  //Array([a] => para11 [b] => para12 [0] => para13 [2] => para14)    $mypara1["c"] = "para15";  //以“$array[]=$value”形式添加    print_r ($mypara1); //Array([a] => para11 [b] => para12 [0] => para13 [2] => para14 [c] => para15) ?>  


 <?php    $mypara = array("para1","para2","para3","para4");    echo array_pop($mypara); //将最后的元素返回被删 In addition to the value of pop, print_r output PHP ($mypara); //Array ([0] => para1 [1]=> para2 [2]=> para3 > 

two, use the array to achieve queue:

1, in the data structure, queue and stack are different, follow the "FIFO" the principle of.

2, an example of the queue, is like a infusion needle, which first enters the body of the fluid.

3, in PHP, regard array as a queue, you can use array_push () function or "$array[]=$value" to add data operations, and use array_shift () function to complete data removal operation.

4, an array delete queue data example:

$mypara =

 < PHP? Array ("a =>"; "para1", "B" => "para2", "C" => "para3"); echo array_shift ($mypara); print_r ($mypara); > 


? Note: PHP also provides an array from the first queue insert one or more elements, the function returns the number of elements inserted, using the format and function of array_push (). That is, the array_unshift () function and the array_shift () function can be used for the operation of the queue.

more PHP on the relevant content of interested readers can see the special station: "PHP data structure and algorithm tutorial " and "PHP " basic grammar tutorial "php object-oriented programming tutorial" , php string (string) " php+mysql database operation tutorial " and "php " common database operation skills summary

hopes the PHP program designed to help to everyone.

This concludes the body part