Array Iterations
foreach Loop
Section titled “foreach Loop”<?php// ============================================// PHP FOREACH LOOP EXAMPLES// ============================================
$fruits = ["Apple", "Banana", "Orange"];
// Basic foreachforeach ($fruits as $fruit) { echo $fruit . PHP_EOL; // Output: Apple Banana Orange}
// foreach with key and value$person = ["name" => "John", "age" => 30, "city" => "NYC"];foreach ($person as $key => $value) { echo "$key: $value" . PHP_EOL; // Output: name: John age: 30 city: NYC}
// foreach by reference (modify original)$numbers = [1, 2, 3];foreach ($numbers as &$num) { $num *= 2;}print_r($numbers); // Output: [2, 4, 6]ArrayIterator
Section titled “ArrayIterator”<?php// ============================================// PHP ARRAYITERATOR EXAMPLES// ============================================
$fruits = ["Apple", "Banana", "Orange"];
// Create ArrayIterator$iterator = new ArrayIterator($fruits);
// Iterateforeach ($iterator as $key => $value) { echo "$key: $value" . PHP_EOL; // Output: 0: Apple 1: Banana 2: Orange}
// Iterator methods$iterator->append("Mango");echo $iterator->count() . PHP_EOL; // Output: 4echo $iterator->current() . PHP_EOL; // Output: Apple
// Seek to position$iterator->seek(2);echo $iterator->current() . PHP_EOL; // Output: OrangeGenerator Functions
Section titled “Generator Functions”<?php// ============================================// PHP GENERATOR FUNCTIONS EXAMPLES// ============================================
// Basic generatorfunction getNumbers() { yield 1; yield 2; yield 3;}
foreach (getNumbers() as $number) { echo $number . PHP_EOL; // Output: 1 2 3}
// Generator with loopfunction getRange($start, $end) { for ($i = $start; $i <= $end; $i++) { yield $i; }}
foreach (getRange(1, 5) as $num) { echo $num . PHP_EOL; // Output: 1 2 3 4 5}
// Key-value generatorfunction getPerson() { yield "name" => "John"; yield "age" => 30; yield "city" => "NYC";}
foreach (getPerson() as $key => $value) { echo "$key: $value" . PHP_EOL; // Output: name: John age: 30 city: NYC}