PHP函数array_shift()在删除数组元素中的用法介绍_PHP教程

WBOY
Release: 2016-07-15 13:29:11
Original
966 people have browsed it

我们在以前的文章中曾经向大家描述过在数组尾端删除数组元素的函数array_pop()的具体用法,今天我们向大家介绍的则是在数组首端删除数组元素的方法。在这里我们将会用到接下来我们就向大家示范PHP函数array_shift()是如何从数组的开头删除元素:

  1. <?   
  2.  
  3. /* 首先我们建立一个数组 */   
  4.  
  5. $fruitArray = array("apple", "orange", "banana", "Peach", "pear");    
  6.  
  7. /* 使用 array_shift()函数从数组的开头删除一个元素 */   
  8.  
  9. $shifted = array_shift($fruitArray);    
  10.  
  11. /* 现在我们把删除后的数组中所有元素的键(key)与值(value)都显示在网页上 */   
  12.  
  13. while (list($key,$value) = each($fruitArray)) {   
  14.  
  15. echo "$key : $value<br>";   
  16.  
  17. }    
  18.  
  19. echo "<br>最后,刚才被删除的元素的值会储存在 $shifted 变量里面,它的值是:   
  20.  
  21. $shifted";    
  22.  
  23. ?>   
Copy after login

显示结果如下: 

0 : orange

1 : banana

2 : Peach

3 : pear 

最后,刚才被PHP函数array_shift()删除的元素的值会储存在 $shifted 变量里面,它的值是:apple 。


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446377.htmlTechArticle我们在以前的文章中曾经向大家描述过在数组尾端删除数组元素的函数array_pop()的具体用法,今天我们向大家介绍的则是在数组首端删除数...
source:php.cn
Statement of this Website
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 [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!