Splitting three comma-delimited strings I've been reading about loops this morning, to try to improve this snippet of code:

$deliveryoutcode1 = array();
$deliveryoutcode1 = explode(",",$rows['deliveryoutcode1']);
$deliveryoutcode2 = array();
$deliveryoutcode2 = explode(",",$rows['deliveryoutcode2']);
$deliveryoutcode3 = array();
$deliveryoutcode3 = explode(",",$rows['deliveryoutcode3']);


I started to read about loops this morning and I came up with this:

$i=1;
while ($i &lt;= 3) { // Output values from 0 to 3
    $deliveryoutcode[$i] = array();
    $deliveryoutcode[$i] = explode(",",$rows['deliveryoutcode' . $i . '']);
    $i++;
}


This works great, but I'm wondering if there is a better way to do this. It appears as though I've come up with the best way but as this is my first attempt at loops, I better ask the experts.
Several things to mention:

1. Use a for loop.

Semantically speaking, a for loop is way more suited in your case than a while loop. for usually means "repeat this for a specified number of steps" while while means "repeat this as long as a given condition is true". Given, you can ALWAYS replace a for with a while from a technical point of view and exactly this is the reason why you need to decide which one to use based on semantics

2. No need to use explicit array initialization.

When you assign the result of explode to your array object the interpretor will take care of the object type. There is no reason in this case to make it explicit so it is better to delete that line.

3. Naming

Not sure if this is part of the exercise or not, but consider using proper names for your objects. What do you explode? What does the array contains? Is a list of houses? Are those candies? Users? deliveryoutcode does not have too much meaning.

If you really don't have any real objects to work on, invent some and use those. It creates good habits for your programming journey.