Recalculate array indices

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Recalculate array indices

Post 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 :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post 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
Post Reply