PHP: Re-index an Array

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.

Easy.

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: http://www.php.net/manual/en/function.array-values.php

You might also enjoy:

About Craig Lotter

Craig Lotter is an established web developer and application programmer, with strong creative urges (which keep bursting out at the most inopportune moments) and a seemingly insatiable need to love all things animated. Living in the beautiful coastal town of Gordon's Bay in South Africa, he games, develops, takes in animated fare, trains under the Funakoshi karate style and for the most part, simply enjoys life with his amazing wife and daughter. Oh, and he draws ever now and then too.
This entry was posted in Technology & Code, Tutorials and tagged , , , , , . Bookmark the permalink.
    blog comments powered by Disqus