Skip to content
Advertisement

While loop on multi-dimensional array

So I have a function that currently has a foreach and it works amazing, but I’m being forced to change it to a while loop:

PLEASE NOTE: The developers at my company don’t want to use the foreach and they think that a while loop would be more efficient, but I’m not understanding how that would be executed, so I need some help.

So I have the following function ($post_blocks is an array of arrays):

JavaScript

It uses the parameters to build up an array as shown below:
Output

So I started building a while loop inside the function, but I’m clueless on how to achieve it without using a foreach or if it’s even possible, so I replaced the foreach with:

JavaScript

All help will be appreciated! It makes no sense to me, but if someone can assist, I’d be forever grateful!

Advertisement

Answer

It’s basically the same as your foreach loop, you just set the iteration variable by indexing the array, and increment the index manually.

JavaScript

I’m not sure why your colleagues think this is preferable.

If there’s a company coding style they want you to follow, why don’t you ask them what it should be?

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement