[英]How to remove an element at the end and add an element at start in array PHP
好的,所以我将头撞在墙上,但无法弄清楚,正在PHP中制作最近的项目列表,我也想添加一个元素以数组的开头,并在我5后在结尾处删除1数组中的元素,但这不起作用
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);
}
}
您需要的是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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.