shasi kanth
shasi kanth

Reputation: 7094

php Reorder an array

i have an array in php like:

$a = array(0=>'a', 1=>'b', 2=>'c', 3=>'d');

Now i unset an element in array:

unset($a[2]);

Now i have the array as:

$a = array(0=>'a', 1=>'b', 3=>'d');

But i want to reorder the array such that the indexes are numerically organized, like:

$a = array(0=>'a', 1=>'b', 2=>'d');

What i can do to get this change?

Upvotes: 0

Views: 306

Answers (2)

Spudley
Spudley

Reputation: 168685

this should do it:

$new_array = array_values($old_array);

Upvotes: 3

alexn
alexn

Reputation: 58962

A solution is to merge your array with an empty array, like so:

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

Upvotes: 1

Related Questions