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

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

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条)

相关推荐 更多相似问题