共計 372 個字符,預計需要花費 1 分鐘才能閱讀完成。
可以使用以下方法交換數組中的元素:
- 使用臨時變量進行交換:
$temp = $array[$index1];
$array[$index1] = $array[$index2];
$array[$index2] = $temp;
- 使用 list() 函數進行交換:
list($array[$index1], $array[$index2]) = array($array[$index2], $array[$index1]);
- 使用 array_splice() 函數進行交換:
$temp = $array[$index1];
$array[$index1] = $array[$index2];
array_splice($array, $index2, 1, $temp);
其中,$array 是要交換元素的數組,$index1 和 $index2 是要交換的元素的索引。
丸趣 TV 網 – 提供最優質的資源集合!
正文完