How to Iterate Over an Array in PHP? Last Updated : 13 Mar, 2025 Comments Improve Suggest changes Like Article Like Report Arrays are fundamental data structures in PHP, and the ability to iterate through them is crucial for manipulating and processing data. In this article, we will explore various approaches to iterate through arrays in PHP.Table of ContentUsing for LoopUsing foreach LoopUsing while Loop with each() FunctionUsing array_walk() with a callback functionUsing array_map() with a Callback FunctionUsing array_reduce()Using for LoopThe PHP for loop is a basic way to iterate through an array. It is useful when you need to access array elements by their index. PHP <?php $arr = [1, 2, 3, 4, 5]; $length = count($arr); for ($i = 0; $i < $length; $i++) { echo $arr[$i] . ' '; } ?> Output1 2 3 4 5 Using foreach LoopThe PHP foreach loop is an method to iterate through an array. There is no need of index number in foreach loop. PHP <?php $arr = [1, 2, 3, 4, 5]; foreach ($arr as $val) { echo $val . ' '; } ?> Output1 2 3 4 5 Using while Loop with each() FunctionThe PHP each() function can be used in combination with a while loop to iterate through an array while maintaining the internal pointer. PHP <?php $arr = ['apple', 'banana', 'cherry']; while ($element = each($arr)) { echo $element['key'] . ' => ' . $element['value'] . "\n"; } ?> Output0 => apple 1 => banana 2 => cherryUsing array_walk() with a callback functionIn PHP, `array_walk()` iterates through an array, applying a user-defined callback function to each element. The callback receives each array element as an argument, allowing for custom operations on array values without needing to manage iteration explicitly. PHP <?php $arr = [1, 2, 3, 4, 5]; // Callback function to print each element function printElement($value) { echo $value . " "; } // Applying the callback function to each element of the array array_walk($arr, 'printElement'); ?> Output1 2 3 4 5 Using array_map() with a Callback FunctionThe array_map() function in PHP applies a callback function to each element of an array, creating and returning a new array with the results of the callback function. PHP <?php $array = [1, 2, 3, 4, 5]; // Callback function to increment each element by 1 function increment($value) { return $value + 1; } // Applying the callback function to each element of the array $newArray = array_map('increment', $array); // Print the new array foreach ($newArray as $val) { echo $val . ' '; } ?> Output2 3 4 5 6 Using array_reduce()This approach uses array_reduce() to apply a callback function to each element of the array, reducing the array to a single cumulative value. PHP <?php $array = array('apple', 'banana', 'cherry'); array_reduce($array, function($carry, $item) { echo $item . "\n"; }); ?> Outputapple banana cherry Comment More infoAdvertise with us Next Article How to Iterate Over an Array in PHP? V vkash8574 Follow Improve Article Tags : PHP PHP-array Geeks Premier League 2023 Similar Reads PHP iterator_to_array() Function The iterator_to_array() function is an inbuilt function in PHP that is used to copy the iterator object (e.g. objects implementing the Iterator or IteratorAggregate interface) into an array. An iterator is an object that allows you to loop through a set of values one at a time without knowing the un 3 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 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 Declare and Initialize 3x3 Matrix in PHP ? PHP, a widely used scripting language, is primarily known for its web development capabilities. However, it also offers a robust set of features to perform various other programming tasks, including the creation and manipulation of matrices. A matrix is a two-dimensional array consisting of rows and 3 min read How to check an element is exists in array or not in PHP ? An array may contain elements belonging to different data types, integer, character, or logical type. The values can then be inspected in the array using various in-built methods : Approach 1 (Using in_array() method): The array() method can be used to declare an array. The in_array() method in PHP 2 min read How to get elements in reverse order of an array in PHP ? An array is a collection of elements stored together. Every element in an array belong to a similar data type. The elements in the array are recognized by their index values. The elements can be subjected to a variety of operations, including reversal. There are various ways to reverse the elements 4 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 ArrayObject getIterator() Function in PHP The getIterator() function of the ArrayObject class in PHP is used to create an iterator from an ArrayObject instance. This iterator can be used to iterate through the array of the respective ArrayObject. Syntax: ArrayIterator getIterator() Parameters: This function does not accepts any parameters. 1 min read 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 PHP | ArrayIterator append() Function The ArrayIterator::append() function is an inbuilt function in PHP which is used to append an element into an array iterator. Syntax: void ArrayIterator::append( mixed $value ) Parameters: This function accepts single parameter $value that holds the value that need to be append. Return Value: This f 1 min read Like