The code in PHP that USES arrays to implement the stack data structure

  • 2020-05-12 02:21:27
  • OfStack

In the stack, the last data to be pushed (into the stack) will be popped (out of the stack) first.
That is, the "first in, then out" data structure is adopted in data storage.
In PHP, the array is treated as a stack, which is mainly done with array_push() and array_pop() system functions.
Pushing is mainly used to add one or more elements to the end of the array of the first parameter using the array_push() function, and then return the length of the new array, as shown below:
 
<?php 
$zhan=array("WEB");// The statement 1 Two arrays as a stack  
array_push($zhan,"PHP");// Pushes a string onto a stack (array)  
array_push($zhan,"WWW.CHHUA.COM");// To push the 1 An element  
print_r($zhan);// Print array contents  
?> 

Pushing is mainly to use the array_pop() function to pop up the last function of the array and subtract the length of the array by 1, as shown below:
 
<?php 
$zhan=array("WEB","www.chhua.com","WEB Development of notes ","PHP"," The website construction ");// The statement 1 Two arrays as a stack  
array_pop($zhan);// To push a string out of an array  
print_r($zhan);// Print array contents  Array([0] => WEB[1] => www.chhua.com[2] => WEB Development of notes [3] => PHP) 
?> 

Related articles: