Skip to main content

Mastering foreach as PHP: The Complete Guide for Beginners and Experts

foreach as php — 5 Real-World Examples You Can Use Today

foreach as php — 5 Real-World Examples You Can Use Today

The foreach loop in PHP (using the as keyword) is the easiest way to iterate arrays and objects. Below are five clear, Blogger-friendly examples (each code block is escaped so it displays correctly in the post).

1) Indexed Array (simple list)

Use this when you have a plain numeric-indexed array.

<?php
$fruits = ["Apple", "Banana", "Cherry"];

foreach ($fruits as $fruit) {
    echo $fruit . "<br>";
}
?>
    

2) Associative Array (key => value)

Great for mapping keys to values (like name => age).

<?php
$person = [
    "name" => "John",
    "age" => 25,
    "city" => "New York"
];

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

3) Multidimensional Array

Use for arrays containing arrays (e.g., list of users).

<?php
$users = [
    ["name" => "Alice", "age" => 22],
    ["name" => "Bob",   "age" => 30]
];

foreach ($users as $user) {
    echo "Name: " . $user["name"] . ", Age: " . $user["age"] . "<br>";
}
?>
    

4) Objects (iterate public properties)

You can loop over an object to read its public properties.

<?php
class Car {
    public $brand = "Toyota";
    public $model = "Corolla";
    public $year  = 2022;
}

$car = new Car();

foreach ($car as $prop => $val) {
    echo $prop . ": " . $val . "<br>";
}
?>
    

5) Mixed Data Types (strings, numbers, booleans)

Handle arrays that contain different data types. Use var_dump() to inspect values when needed.

<?php
$mixed = ["Hello", 123, 3.14, true, null];

foreach ($mixed as $item) {
    var_dump($item);   // shows type + value
    echo "<br>";
}
?>
    

Comments

Popular posts from this blog

PHP array_column() Function

  <?php // An array that represents a possible record set returned from a database $a =  array (    array (      'id'  =>  5698 ,      'first_name'  =>  'Mehran' ,      'last_name'  =>  'Yaqoob' ,   ),    array (      'id'  =>  4767 ,      'first_name'  =>  'Muneeb' ,      'last_name'  =>  'Ahmad' ,   ),    array (      'id'  =>  3809 ,      'first_name'  =>  'Uzair' ,      'last_name'  =>  'Rajput' ,   ) ); $last_names = array_column($a,  'last_name' ); print_r($last_names); ?> Output: Array (   [0] => Yaqoob   [1] => Ahmad   [2] => Rajput ) Syntex: array_column( array ,  column_key ,  index_key ...

Mastering PHP's array_merge(): When to Use It (And When Not To)

Pros, Cons, and Best Practices PHP's  array_merge()  function is one of the most commonly used array functions in web development. Whether you're building a simple website or a complex web application, understanding how to effectively merge arrays can save you time and prevent headaches. In this comprehensive guide, we'll explore everything you need to know about  array_merge() , including its advantages, limitations, practical use cases, and alternatives. What is  array_merge()  in PHP? array_merge()  is a built-in PHP function that combines two or more arrays into a single array. The function takes multiple array arguments and returns a new array containing all the elements from the input arrays. Basic Syntax php array_merge ( array ... $arrays ) : array Simple Example php $array1 = [ 'a' , 'b' , 'c' ] ; $array2 = [ 'd' , 'e' , 'f' ] ; $result = array_merge ( $array1 , $array2 ) ; print_r ( $result ) ; /* O...