Home  >  Article  >  Backend Development  >  关于PHP堆栈与列队的学习_php技巧

关于PHP堆栈与列队的学习_php技巧

WBOY
WBOYOriginal
2016-05-17 08:59:54918browse

在PHP中数组常被当作堆栈(后进先出:LIFO)与队列(先进先出:FIFO)结构来使用。PHP提供了一组函数可以用于push与pop(堆栈)还有shift与unshift(队列)来操作数组元素。堆栈与列队在实践中应用非常广泛。
我们可以先看下堆栈:
 

复制代码 代码如下:

    $arr = array();
   array_push($arr,'aaa');
   array_push($arr,'bbb');
   $arr.pop();
   print_r($arr);
?>
 

如果你打算把数组作为队列来使用(FIFO),你可以使用array_unshift()来增加元素,使用array_shift()删除:
复制代码 代码如下:

   $arr = array();
   array_unshift($arr,'aaa');
   array_unshift($arr,'bbb');
   print_r($arr);
   array_shift($arr);
   print_r($arr);
?>

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