How to Loop Through an Array using a foreach Loop in PHP? Last Updated : 24 Apr, 2024 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 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 How to convert an array to CSV file in PHP ? To convert an array into a CSV file we can use fputcsv() function. The fputcsv() function is used to format a line as CSV (comma separated values) file and writes it to an open file. The file which has to be read and the fields are sent as parameters to the fputcsv() function and it returns the leng 2 min read What is the difference between for and Foreach loop in PHP ? Loops can be used to iterate over collection objects in PHP. The for and foreach loop can be used to iterate over the elements. for loopThe for loop works at the end of the given condition. It is used for the implementation of variables and works in a single way. The for loop does not work in the ca 3 min read How to Slice an Array in PHP? In PHP, slicing an array means taking a subset of the array and extracting it according to designated indices. When you need to extract a subset of elements from an array without changing the original array, this operation comes in handy. PHP comes with a built-in function called array_slice() to he 2 min read How to Switch the First Element of an Arrays Sub Array in PHP? Given a 2D array where each element is an array itself, your task is to switch the first element of each sub-array with the first element of the last sub-array using PHP.Example:Input: num = [ ['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']];Output: [ ['g', 'b', 'c'], ['d', 'e', 'f'], ['a', 'h', ' 2 min read How to Insert a New Element in an Array in PHP ? In PHP, an array is a type of data structure that allows us to store similar types of data under a single variable. The array is helpful to create a list of elements of similar types, which can be accessed using their index or key.We can insert an element or item in an array using the below function 5 min read Like