How to Loop Through an Array using a foreach Loop in PHP? Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report Given an array (indexed or associative), the task is to loop through the array using foreach loop. The foreach loop iterates through each array element and performs the operations. PHP foreach LoopThe foreach loop iterates over each key/value pair in an array. This loop is mainly useful for iterating through associative arrays where you need both the key and the value. The foreach loop iterates over an array of elements, the execution is simplified and finishes the loop in less time comparatively. The foreach loop works for both indexed and associative arrays. Example 1: In this example, we will use foreach loop to iterate over an indexed array. PHP <?php // Indexed array $arr = array(10, 20, 30, 40, 50); foreach ($arr as $element) { echo $element . " "; } ?> Output10 20 30 40 50 Example 2: In this example, we will use foreach loop to iterate over an associative array. PHP <?php // Associative array $student_marks = array( "Maths" => 95, "Physics" => 90, "Chemistry" => 96, "English" => 93, "Computer" => 98 ); foreach ($student_marks as $key => $value) { echo "$key => $value\n"; } ?> OutputMaths => 95 Physics => 90 Chemistry => 96 English => 93 Computer => 98 Create Quiz Comment B blalverma92 Follow 0 Improve B blalverma92 Follow 0 Improve Article Tags : PHP PHP-Questions Explore PHP Tutorial 8 min read BasicsPHP Syntax 4 min read PHP Variables 5 min read PHP | Functions 8 min read PHP Loops 4 min read ArrayPHP Arrays 5 min read PHP Associative Arrays 4 min read Multidimensional arrays in PHP 5 min read Sorting Arrays in PHP 4 min read OOPs & InterfacesPHP Classes 2 min read PHP | Constructors and Destructors 5 min read PHP Access Modifiers 4 min read Multiple Inheritance in PHP 4 min read MySQL DatabasePHP | MySQL Database Introduction 4 min read PHP Database connection 2 min read PHP | MySQL ( Creating Database ) 3 min read PHP | MySQL ( Creating Table ) 3 min read PHP AdvancePHP Superglobals 6 min read PHP | Regular Expressions 12 min read PHP Form Handling 4 min read PHP File Handling 4 min read PHP | Uploading File 3 min read PHP Cookies 9 min read PHP | Sessions 7 min read Like