A while loop is used to run a set of statements multiple times without stopping to collect the output. We use it extensively to run PHP code in a loop, but we aren’t always the most familiar with it. We should at least be aware of the syntax of while loops.
PHP has a while loop syntax that would make this much easier to learn. However, I think in the interest of clarity and simplicity, we should stick with the while loop.
While loops are a way to run multiple statements. In our case, we are looping over a PHP array of arrays. Each array in the array contains multiple lines of code that run a single statement. For example, line 1 in each array is the line that executes the statement. line 2 is the line of the array that contains the statement. Each time through the loop, the statements are executed line by line, one after another.
This is a great example of why you shouldn’t make new code. It’s easy to try to make things harder than they are. In our case, it was a lot of code and a lot of lines of code. It would have been easy to say “just use a while loop.” Instead of using a while loop, we used a for loop. And that is a good thing.
While loops are an easy way to loop through the array. The advantage is that you can do a great deal of pre-processing to make the code as efficient as possible. However, they are still relatively inefficient compared to using a list.
As I mentioned earlier, a list is easier to navigate and change. So while it’s true that looping through a list is a lot easier, it’s also a lot harder to make your code efficient and readable when you’re looping through it. Once again, pre-processing makes a lot of things simple.
If you want to do something as easy as looping through an array, you can use a while loop. But if you want to do something as complex as looping through an array (or even an array of arrays), you can use a while loop that does a lot of branching.
A while loop only loops through the elements youre looping through. In our case, we want the entire list of Visionaries to be looped through, so we need to get rid of the while loop. The easiest way to do this is to use the array_map function. It takes an array of values and maps each element to a new array. That means if the original array has 3 values, the new array will have 2 values.
Another way to do it is to loop through the array twice. With the array_values function, you can get the array element by element, and then loop through that in the same way you would a normal array. In our case, we loop through the array three times, but the second time we loop through the array once. The result is that the new array has 4 values, and the array_values function will loop through each element with two values.