开发者

Making sure a PHP array has only sequential keys [duplicate]

开发者 https://www.devze.com 2022-12-19 05:20 出处:网络
This question already has answers here: How do you reindex an array in PHP but with indexes starting from 1?
This question already has answers here: How do you reindex an array in PHP but with indexes starting from 1? (12 answers) Closed 5 months ago. 开发者_如何学Go

Is there any way to have an array with items like:

$array[1];
$array[3];
$array[4];

Be turned into an array like this:

$array[1];
$array[2];
$array[3];

So that the array has only sequential numeric keys? Thanks!


array_values() returns all the values from the input array and indexes numerically the array.


You can call array_values() on that array, and it will return a newly indexed array. (This assumes you have a numerically indexed array of course)


$array = array_merge($array, array());

As long as $array has numerical keys, it will reorder them (starting from 0), numerically.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号