๐Ÿ” PHP Control Flow & Logic
Estimated reading: 3 minutes 33 views

๐Ÿ”„ PHP Foreach Loop โ€“ Iterate Arrays & Objects Easily

A focused guide on using the foreach loop to iterate through arrays and objects in PHP.


๐Ÿงฒ Introduction โ€“ Why Use the foreach Loop?

The foreach loop is the cleanest and most efficient way to loop through arrays and objects in PHP. It eliminates the need for index management and is especially powerful when working with associative arrays or collections.

๐ŸŽฏ In this guide, youโ€™ll learn:

  • Syntax of foreach for both indexed and associative arrays
  • How to access keys and values
  • Best practices for clean iteration
  • Real-world examples and use cases

โœ… Basic foreach Syntax

$colors = ['red', 'green', 'blue'];

foreach ($colors as $color) {
    echo "Color: $color <br>";
}

๐Ÿ” Output:

Color: red
Color: green
Color: blue

๐Ÿ“˜ Explanation: Loops through each value in the array and assigns it to $color.


๐Ÿ”‘ Accessing Keys and Values

$user = ['name' => 'Alice', 'email' => 'alice@example.com'];

foreach ($user as $key => $value) {
    echo "$key: $value <br>";
}

๐Ÿ” Output:

name: Alice
email: alice@example.com

๐Ÿ“˜ Used for associative arrays where both key and value matter.


๐Ÿ” Foreach with Nested Arrays

$products = [
    ['name' => 'Laptop', 'price' => 1000],
    ['name' => 'Mouse', 'price' => 25],
];

foreach ($products as $product) {
    echo "Product: {$product['name']}, Price: {$product['price']} <br>";
}

๐Ÿ” Output:

Product: Laptop, Price: 1000
Product: Mouse, Price: 25

๐Ÿ“˜ Access individual values in a multidimensional array.


๐Ÿ“ฆ Using foreach on Objects

class User {
    public $name = "John";
    public $email = "john@example.com";
}

$user = new User();

foreach ($user as $property => $value) {
    echo "$property: $value <br>";
}

๐Ÿ” Output:

name: John
email: john@example.com

๐Ÿ“˜ Iterates over public properties of an object.


โ›” Using break and continue in foreach

๐Ÿ”‚ Skip an Iteration

$nums = [1, 2, 3, 4];

foreach ($nums as $n) {
    if ($n === 3) continue;
    echo "$n ";
}

๐Ÿ” Output: 1 2 4


๐Ÿšซ Exit the Loop Early

foreach ($nums as $n) {
    if ($n === 3) break;
    echo "$n ";
}

๐Ÿ” Output: 1 2


๐Ÿง  Best Practices

  • โœ… Use foreach for all array traversals โ€” cleaner and faster
  • โœ… Avoid modifying the array inside the loop
  • โŒ Donโ€™t unset elements while iterating (use array_filter instead)
  • โœ… Use descriptive key/value variable names ($key => $value, not $k => $v)

๐Ÿ“Œ Summary โ€“ Recap & Next Steps

The foreach loop is your go-to tool for handling arrays and objects in PHP. Itโ€™s readable, safe, and works great with key/value pairs.

๐Ÿ” Key Takeaways:

  • Use foreach for array and object iteration
  • Supports key-value mapping in associative arrays
  • Works with both indexed and nested arrays
  • Use break and continue for flow control

โš™๏ธ Real-World Use Cases:
Rendering menus, processing API responses, form data, database results, and nested loops for reports.


โ“ Frequently Asked Questions (FAQs)

โ“ Is foreach better than for for arrays?
โœ… Yes. foreach is faster and cleaner for arrays.

โ“ Can I modify array elements inside foreach?
โœ… Yes, but use reference syntax:

foreach ($array as &$value) {
    $value *= 2;
}

โ“ Does foreach work on objects?
โœ… Yes, but only iterates over public properties.

โ“ How to skip an item in foreach?
โœ… Use continue; to skip to the next iteration.

โ“ Can I break out of a foreach loop?
โœ… Yes. Use break; when a condition is met.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

๐Ÿ”„ PHP Foreach Loop

Or Copy Link

CONTENTS
Scroll to Top