How to Loop Through an Array using a foreach Loop in PHP? Last Updated : 24 Apr, 2024 Summarize Comments Improve Suggest changes Share 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 Comment More infoAdvertise with us Next Article How to Loop Through an Array using a foreach Loop in PHP? B blalverma92 Follow Improve Article Tags : PHP PHP-Questions Similar Reads How to find the index of an element in an array using PHP ? In this article, we will discuss how to find the index of an element in an array in PHP. Array indexing starts from 0 to n-1. Here we have some common approachesTable of ContentUsing array_search() FunctionUsing array_flip()Using a custom functionUsing a foreach LoopUsing array_keys FunctionUsing ar 6 min read How to Break Foreach Loop in PHP? Breaking a foreach loop consists of terminating the execution of the loop prematurely based on a certain condition. In this article, we will explore and learn two approaches to breaking for each loop in PHP.Below are the approaches to break for each loop in PHP:Table of ContentUsing break KeywordUsi 3 min read How to check foreach Loop Key Value in PHP ? In PHP, the foreach loop can be used to loop over an array of elements. It can be used in many ways such asTable of ContentUsing the for-each loop with simple valuesUsing the foreach loop with Key-Value pairsUsing array_keys Function to Access Keys and ValuesUsing array_walk Function for IterationUs 3 min read How to display array structure and values in PHP ? In this article, we will discuss how to display the array structure and values in PHP. To display the array structure and its values, we can use var_dump() and print_r() functions. It includes Array sizeArray valuesArray value with IndexEach value data type We will display the array structure using 2 min read How to create an array with key value pairs in PHP? In PHP, an array with key-value pairs is called an associative array. It maps specific keys to values, allowing you to access elements using custom keys rather than numerical indices. Keys are strings or integers, while values can be of any data type.Here we have some common approaches to create an 2 min read Like