What is the PHP built-in function to “compress” or “defragment” an array?

I know that it would be trivial to code yourself, but it’s in the interest of not having more support code if it is already built into PHP, is there a built-in function to “compress” the PHP array? In other words, let's say I create an array this way:

$array = array();
$array[2000] = 5;
$array[3000] = 7;
$array[3500] = 9;

What I want is an array, where $ array [0] == 5, $ array [1] == 7, $ array [2] == 9.

I could do this:

function array_defragment($array) {
    $squashed_array = array();
    foreach ($array as $item) {
        $squashed_array[] = $item;
    }
    return $squashed_array;
}

... but it looks like it will be built into PHP - I just can't find it in the docs.

+1
source share
1 answer

Just use array_values:

$array = array();
$array[2000] = 5;
$array[3000] = 7;
$array[3500] = 9;

$array = array_values($array);
var_dump($array === array(5, 7, 9));
+12
source

Source: https://habr.com/ru/post/1741109/


All Articles