How to Iterate PHP Arrays  screenshot

How to Iterate PHP Arrays

How to Iterate PHP Arrays

In PHP, arrays are the probably the most used variables because they assist programmers to store multiple values with a single name. One can think of a PHP array as a holder of many values that can be accessed by use of an index. While arrays may seem simple to use, they only make sense when iterated or looped to determine their values or to attach values to them. For instance, when retrieving data from a database table, values are first stored by a variable and can be accessed through iteration.

PHP arrays are created through the “array ()” language construct. Values are enclosed on parenthesis separated by commas. For instance, to create an array that holds the name of 3 users. You can write the code as follows and output the values using the PHP print_r function:

<?php
$users = array(
    "Jimmy",
    "Francis",
    "Mary"
);
print_r($users);
?>

As seen above, the array “$users” holds three values: Jimmy, Francis and Mary. To get the array values, you need to use the array key. For example, to get “Jimmy”, you will need to echo out the array value in the first position. PHP stores array values starting from key Zero (0). So, we can get the value of our first user Jimmy by using the following code. Note: we are enclosing the array key inside square brackets.

<?php
$users = array(
    "Jimmy",
    "Francis",
    "Mary"
);
echo $users[0];
?>

Now, this method of outputting array values is simple and straightforward but is tedious to use in real life applications. An example is a situation where an array holds thousands of values. In such a scenario, typing the array keys one by one may not be the best solution. Instead, one should use array loops or iterations.

PHP foreach loop statement

This form of iteration is used to iterate through key/value pairs in arrays. The foreach statement assigns the current value of the array to the variable that you specify e.g. $arrayvalue. Then, you can echo out the value each time the foreach statement iterates as show below:

<?php
$users = array(
    "Jimmy",
    "Francis",
    "Mary"
);
foreach ($users as $arrayvalue) {
    echo $arrayvalue . "";
}
?>

PHP for… loop statements

Another option of iterating through PHP array values is using the PHP “for….loop” statement. In this case, you must know the total count of values in each array using the count () function. The PHP for loop statement takes the following form:

<?php
for (initcounter;testcounter;incrementcounter) {
    //code to be executed;
    
}
?>

• Init counter represents the first key value where iteration will begin.

• Test counter is performed in each statement and if it returns a positive value, the loop goes on.

• Finally, the increment counter increases the loop value by one in every iteration.

So we can re-code our iteration above using this formula as seen below:

<?php
$users = array(
    "Jimmy",
    "Francis",
    "Mary"
);
for ($x = 0;$x
?>

PHP while loop statement

The PHP while loop statement is another great statement that tells PHP to iterate an array several times. The while” loop” statement only loops when a certain condition is true.

<?php
While (conditionistrue) {
    //Statement to execute;
    
}
?>

In our case above, we will iterate through our sample array until we echo out all the users. We will still use the count () function to aid the loop.

<?php
$users = array(
    "Jimmy",
    "Francis",
    "Mary"
);
$x = 0;
While ($x
?>

Other variation of while loop is using reset function:

<?php
$users = array(
    "Jimmy",
    "Francis",
    "Mary"
);
reset($users);
While (list($key, $val) = each($users)) {
    echo $val . "";
}
?>

Reset function sets the array’s pointer to its first element. Each function returns current key and value from array and moves next position the pointer of array.

I hope you enjoyed reading this article about PHP array iterations and take the steps further in writing complex loops to aid your programming even in mission critical applications.