array_splice()
用於提取和插入數組中的元素序列。
array_splice($array, 0, 0, array_splice($array, -3));
array_splice($array, -3)
刪除數組的最後3個元素,並返回作爲數組。然後array_splice($array, 0, 0, <that>)
將該數組插入數組的開頭。
請注意,這不會保留關聯數組的鍵,這在您的示例中似乎是必需的。對於這一點,你可以使用在documentation notes
function array_rotate($array, $shift) {
if(!is_array($array) || !is_numeric($shift)) {
if(!is_array($array)) error_log(__FUNCTION__.' expects first argument to be array; '.gettype($array).' received.');
if(!is_numeric($shift)) error_log(__FUNCTION__.' expects second argument to be numeric; '.gettype($shift)." `$shift` received.");
return $array;
}
$shift %= count($array); //we won't try to shift more than one array length
if($shift < 0) $shift += count($array);//handle negative shifts as positive
return array_merge(array_slice($array, $shift, NULL, true), array_slice($array, 0, $shift, true));
}
嘗試流行,並在臨時數組中添加這3個項目,然後用array_unshift()前面加上他們。 – NoImaginationGuy
只是一個友好的提示,你可能想要閱讀本頁:[如何 - 問問指南](https://stackoverflow.com/help/how-to-ask),所以你可以隨時確保你的問題很容易回答並且儘可能清楚。一定要包括你爲解決你遇到的問題所做的任何努力,以及當你嘗試修復這些問題時發生了什麼。另外不要忘記你的代碼和任何錯誤信息! –