PHP對於陣列的處理提供了很多原生函式,讓開發者可以很方便的去處理陣列裡面的內容

array_splice就是好用的陣列函式之一,可以幫你移除或取代陣列內容

留下前兩筆

$arr = array("Zero","One","Two","Three","Four");
//Zero,One
array_splice($arr, 2);
print_r($arr);

留下第一筆與最後一筆

$arr = array("Zero","One","Two","Three","Four");
//Zero,Four
array_splice($arr, 1, -1);
print_r($arr);

刪除第一筆之後的兩筆

$arr = array("Zero","One","Two","Three","Four");
//Zero,Three,Four
array_splice($arr, 1, 2);
print_r($arr);

取代第一筆之後的兩筆

$arr = array("Zero","One","Two","Three","Four");
//Zero,Five,Six,Three,Four
array_splice($arr, 1, 2, array("Five","Six"));
print_r($arr);
Categories: PHP