`

添加和删除数组元素

    博客分类:
  • PHP
阅读更多

1.在数组头添加元素——array_unshift()

使用该函数,所有已有的数值键都会相应的修改,以反映骑在数组中的新位置,但是关联键不受影响。

$names = array("tom", ”jack“, "jerry");

array_unshift($names, "amy", "job");    //$names = array("amy", "job", "tom", "jack", "jerry");

 

2.在数组尾添加元素——array_push();

该函数跟array_unshift()对应,只是将元素添加在数组尾部。

$names = array("tom", ”jack“, "jerry");

array_push($names, "amy", "job");    //$names = array("tom", "jack", "jerry""amy", "job", );

 

3.从数组头删除元素——array_shift()

array_shift()函数删除并返回数组中找到的第一个元素,其结果是,如果使用的是数组键,则所有相应的值都会下移,而使用关联键的数组不受影响。

$states = array("Ohio", "New York", "Texas" );

$state = array_shift($states);    //$state = "Ohio"

 

4.从数组尾删除元素——array_pop()

删除并返回数组的最后一个元素

$state = array_pop($states);   //$state = "Texas"

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics