php – How to sort the position of an array automatically?

Question:

Example I have:

$matriz = array("Tomate", "Morango", "Melancia");

I would have:

(
    [0] => Tomate
    [1] => Morango
    [2] => Melancia
)

But if I do:

unset($matriz[0]);

I will have:

(     
        [1] => Morango
        [2] => Melancia
)

How to automatically order them back from 0

(     
        [0] => Morango
        [1] => Melancia
)

Answer:

Just create the array again:

$newArray = array_values($oldArray);
Scroll to Top