Page 1 of 1

Recalculate array indices

Posted: Sun Apr 16, 2006 9:03 pm
by alex.barylski
Last question for the night I promise :)

I have an array which has elements in it...surprise surprise :P

Anyways, I remove NULLed elements using array_filter() because it when called without a callback removes any elements which are FALSE.

When the array is cleared of those elements...I am frequently stuck with indicies like:

7, 9, 12, 13, 14, 35

The problem with this, is that indexing an array using a for loop becomes tedious using just a simple index increment...

I could for each the whole thing, but I would prefer to re-calculate the array indices sot they start at zero and end at whatever - index preservation isn't important here :)

I can't use any function other than array_filter() so array_splice() or which ever function re-calculates indices when elements are removed won't work!!!

Any ideas?

Are there any array functions which reset numeric indices???

If there is NO php function which does this, it's faster (in terms of code execution) to just use a for each...

So unless you know a lightning quick method...there is no point :(

Cheers :)

Posted: Sun Apr 16, 2006 9:16 pm
by feyd

Posted: Sun Apr 16, 2006 9:20 pm
by alex.barylski
Cool...

I swear I looked at that function, but having been at my computer for almost 20 hours straight...I'm probably starting to loose it :P