dpl3350 2012-12-14 03:08
浏览 111
已采纳

数组删除第一个元素并在末尾添加一个新元素

If I had the following array:

Array(
    [0] => 30
    [1] => 60
    [2] => 2
    [3] => 55
    [4] => 1
    [5] => 20
    [6] => 1
    [7] => 8
    [8] => 38
    [9] => 58
    [10] => 12
)

How would I delete the first element and then add a new element to the end. So the new array would look like this:

Array(
    [0] => 60
    [1] => 2
    [2] => 55
    [3] => 1
    [4] => 20
    [5] => 1
    [6] => 8
    [7] => 38
    [8] => 58
    [9] => 12
    [9] => 10
)

I could use array_push to add to the end but then how would I delete the first element and reposition the keys?

  • 写回答

2条回答 默认 最新

  • dsdsds12222 2012-12-14 03:09
    关注

    array_shift will remove the first element, and shift all the keys "up" one.

    $array = ('0' => '30', '1' => '20', '2' => '5');
    $array = array_shift($array, '19');
    $var_dump ($array); // gives 0 => 20, 1 => 5, 2 => 19
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题