Example of PHP array based stack and queue functionality

  • 2021-09-04 23:45:32
  • OfStack

This paper illustrates the stack and queue functions of PHP based on arrays. Share it for your reference, as follows:

Stack and queue are two kinds of data structures, which are widely used containers for storing data. Next, let's talk about the application of these two containers in PHP:

1. Implement the stack using arrays:

1. In the stack container, the last stack will be first out of the stack, that is, the so-called "first in, then out" data structure.

2. In PHP, the array is treated as a stack, which can be used array_push() Function or with " $array[]=$value "Complete the stack operation, using the array_pop() Function to complete the stack-out operation.

3. The stack operation is equivalent to putting the data into a bucket-shaped container one by one (assuming that the data and this bucket have appropriate surface area, that is, they can be put horizontally). As a result, after all the data are put into the stack, the advanced stack is at the bottom.

4. Example of array stacking:


<?php
   $mypara = array("para1");
   echo(array_push($mypara,"para2"));  // Add 1 Data to mypara Array 
   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";  // With " $array[]=$value "Formal addition 
   print_r ($mypara1); //Array([a] => para11 [b] => para12 [0] => para13 [2] => para14 [c] => para15)
?>

5. Example of array stacking:


<?php
   $mypara = array("para1","para2","para3","para4");
   echo array_pop($mypara); // Pop up the last element to return the deleted value and output PHP
  print_r($mypara); //Array([0] => para1 [1]=>para2 [2]=>para3)
?>

2. Using arrays to implement queues:

1. In the data structure, queues and stacks are different and follow the principle of "first in, first out".

2. For example, the queue is just like an infusion needle tube, in which the liquid entering the thin tube first enters the human body first.

3. In PHP, the array is treated as a queue, which can be used array_push() Function or with " $array[]=$value "Complete the Add Data operation, using the array_shift() Function to complete the data deletion operation.

4. Example of array deletion queue data:


<?php
   $mypara = array("a"=>"para1","b"=>"para2","c"=>"para3");
   echo array_shift($mypara);
   print_r($mypara);
?>

5. Note: PHP also provides another one to insert one or more elements from the beginning of the queue array. Successful execution of this function will return the number of inserted elements, using format and function array_push() 1 sample. You can use the array_unshift() Function sum array_shift() Function to operate on the queue.

For more readers interested in PHP related content, please check the topics on this site: "PHP Data Structure and Algorithm Tutorial", "PHP Basic Syntax Introduction Tutorial", "php Object-Oriented Programming Introduction Tutorial", "php String (string) Usage Summary", "php+mysql Database Operation Introduction Tutorial" and "php Common Database Operation Skills Summary"

I hope this article is helpful to everyone's PHP programming.


Related articles: