PHP Array Loop

Examples on How to Loop Through PHP Arrays

Here you will learn how to loop through an array using PHP’s various loops whiledo-whilefor, and foreach.

Table of Contents #

Loop Through PHP “Numeric” & “Associative” Arrays

In the following array looping examples you can see the count() function which is used to count the number of elements of an array.

1. “for Loop” with PHP Array

The for loop is a common choice for iterating through arrays with numeric keys. It allows you to specify a starting point, a condition for looping, and an increment or decrement step. Here’s a simple example:

<?php
// Numeric array
$fruits = ["Apple", "Banana", "Mango", "Watermelon"];

for ($i = 0; $i < count($fruits); $i++) {
    echo "{$fruits[$i]}\n";
}

/** Output **
    Apple      
    Banana     
    Mango      
    Watermelon
*/
<?php
//Associative array
$user = [
    "name" => "Akash",
    "email" => "[email protected]",
    "age" => 16
];

$userKeys = array_keys($user);

for ($i = 0; $i < count($userKeys); $i++) {
    echo "{$userKeys[$i]} => {$user[$userKeys[$i]]} \n";
}

/** Output **
    name => Akash
    email => [email protected] 
    age => 16
*/

2. “foreach Loop” with PHP array

The foreach loop is designed for iterating through arrays with both numeric and associative keys. It automatically advances the internal array pointer and assigns each value to a variable. Here’s an example:

<?php
// Numeric array
$fruits = ["Apple", "Banana", "Mango", "Watermelon"];

foreach ($fruits as $fruit) {
    echo "$fruit \n";
}

/** Output **
    Apple      
    Banana     
    Mango      
    Watermelon
*/
<?php
//Associative array
$user = [
    "name" => "Radhe",
    "email" => "[email protected]",
    "age" => 18
];

foreach ($user as $key => $value) {
    echo "$key => $value \n";
}

/** Output **
    name => Radhe
    email => [email protected] 
    age => 18
*/

3. “while Loop” with PHP array

The while loop is useful when you don’t know the number of iterations in advance. It continues looping as long as a specified condition remains true. Example:

// Numeric array
$fruits = ["Apple", "Banana", "Mango", "Watermelon"];

$i = 0;
while($i < count($fruits)){
    echo "{$fruits[$i]}\n";
    $i++;
}

/** Output **
    Apple      
    Banana     
    Mango      
    Watermelon
*/
<?php
//Associative array
$user = [
    "name" => "John",
    "email" => "[email protected]",
    "age" => 24
];

$userKeys = array_keys($user);

$i = 0;
while ($i < count($userKeys)) {
    echo "{$userKeys[$i]} => {$user[$userKeys[$i]]} \n";
    $i++;
}

/** Output **
    name => John
    email => [email protected] 
    age => 24
*/

4. “do-while Loop” with PHP array

Similar to the while loop, the do...while loop guarantees that the loop body executes at least once, as the condition is checked after the loop. Here’s an example:

<?php
// Numeric array
$fruits = ["Apple", "Banana", "Mango", "Watermelon"];

$i = 0;
do {
    echo "{$fruits[$i]}\n";
    $i++;
} while ($i < count($fruits));

/** Output **
    Apple      
    Banana     
    Mango      
    Watermelon
*/
<?php
//Associative array
$user = [
    "name" => "Rahul",
    "email" => "[email protected]",
    "age" => 26
];

$userKeys = array_keys($user);

$i = 0;
do {
    echo "{$userKeys[$i]} => {$user[$userKeys[$i]]} \n";
    $i++;
} while ($i < count($userKeys));

/** Output **
    name => Rahul
    email => [email protected] 
    age => 26
*/

Array Functions for Iteration

PHP provides several array functions that simplify array iteration and manipulation. Functions like array_map, array_filter, and array_reduce allow you to perform operations on array elements without writing explicit loops.

Best Practices

  • Choose the appropriate loop type based on your array’s structure and your specific requirements.
  • Avoid modifying the array while looping to prevent unexpected behavior.
  • Utilize array functions for complex operations, as they can make your code more concise and maintainable.