Answering the question ...
Whenever you want to ignore the keys of an array, just use the function:
array_values ( array $input ): array
Demonstrating in your code:
$array = [
0 => 'Fellipe',
1 => 'Fábio',
2 => 'Mateus',
3 => 'Gustavo'
];
unset($array[2]);
$array = array_values( $array );
Upshot:
Array
(
[0] => Fellipe
[1] => Fábio
[2] => Gustavo
)
See the code working on IDEONE.
... and proposing a simpler alternative:
You can remove an item and keep sorting at once with this function:
array_splice( array &$input, int $offset [, int $length [, mixed $replacement ]] ): array
Applying in your code, just this:
$array = [
0 => 'Fellipe',
1 => 'Fábio',
2 => 'Mateus',
3 => 'Gustavo'
];
array_splice( $array, 2, 1 );
See working on IDEONE.
In the case, the 2
is the initial item to be removed, and the 1
the amount to be removed.
Important:the array_splice
works directly on array. In this case it should not be used $array = array_splice( $array, 2, 1 );
, otherwise the effect will be the reverse. The return is what has been removed, not what is left.
I believe that ksort/uksort does this without problems.
– Inkeliz
@Inkeliz from what I understand, he wants to reindexar the keys ( leave
0
1
2
, and not0
1
3
, missing the2
which has been removed). In this case thearray_values
.– Bacco