如何在结尾处删除元素并在数组PHP中的start处添加元素

时间:2013-04-01 17:41:28

标签: php arrays

好吧所以我正在撞墙,但是我无法解决这个问题,我正在PHP中创建最近的项目列表,我想要添加一个元素来启动数组并在结束后删除1数组中有5个元素,但这不起作用

if(!isset($_SESSION['recent_items'])) {
    $_SESSION['recent_items'] = array();
}

if(isset($_SESSION['recent_items'])) {
    if(count($_SESSION['recent_items']) <= 4) {
        array_push($_SESSION['recent_items'], $script_id);
    } else {
        array_shift($_SESSION['recent_items']);
        array_unshift($_SESSION['recent_items'], $script_id);
    }
}

1 个答案:

答案 0 :(得分:0)

您需要的是array_pop而不是array_shift

if(!isset($_SESSION['recent_items'])) {
    $_SESSION['recent_items'] = array();
}

if(isset($_SESSION['recent_items'])) {
    if(count($_SESSION['recent_items']) <= 4) {
        array_push($_SESSION['recent_items'], $script_id);
    } else {
        array_pop($_SESSION['recent_items']);
        array_unshift($_SESSION['recent_items'], $script_id);
    }
}
相关问题