PHP: Re-index an Array CodeUnit 03 APR 2010

Say you used to have a nice little array which you’ve since gone and hacked to pieces through various merges, and cuttings, etc., resulting is a rather non-uniform, keyed array mess.

Now in order to return to some sort of sanity it might be a good idea to take that input array of yours and retrieve from it a new array containing all the values (currently finding themselves inhabiting the original mess) but with a bright and shiny new, numerically ordered index.


Simply make use of the handy PHP array_values function which basically takes an input array, retrieves all values from it and then creates a new array with all those values tucked behind a nice sequential numeric index.

So for example, an array looking like this {a->tomato,b->apple} would now become {0->tomato,1->apple}.

It really is that simple.

Related Link:

Related Posts:

About Craig Lotter

South African software architect and developer at Touchwork. Husband to a cupcake baker and father to two little girls. I don't have time for myself any more.