在PHP中,有以下幾種方法可以重新索引數(shù)組:
$originalArray = array("a", "b", "c");
$reindexedArray = array_values($originalArray);
print_r($reindexedArray);
輸出:
Array
(
[0] => a
[1] => b
[2] => c
)
$originalArray = array("a", "b", "c");
$reindexedArray = array();
foreach ($originalArray as $value) {
$reindexedArray[] = $value;
}
print_r($reindexedArray);
輸出:
Array
(
[0] => a
[1] => b
[2] => c
)
$originalArray = array("a", "b", "c");
$keysArray = range(0, count($originalArray) - 1);
$reindexedArray = array_combine($keysArray, $originalArray);
print_r($reindexedArray);
輸出:
Array
(
[0] => a
[1] => b
[2] => c
)
無論使用哪種方法,都可以重新索引數(shù)組并獲得以連續(xù)整數(shù)作為鍵名的新數(shù)組。