Iterate associative array using foreach loop in PHP
Given two arrays arr1 and arr2 of size n. The task is to iterate both arrays in the foreach loop. Both arrays can combine into a single array using a foreach loop.
Array: Arrays in PHP is a type of data structure that allows to storing multiple elements of similar data type under a single variable thereby saving the effort of creating a different variable for every data. The arrays are helpful to create a list of elements of similar types, which can be accessed by using their index or key.
Example:
Input : $sides = array('Up', 'Down', 'Left', 'Right')
$directions = array('North', 'South', 'West', 'East')
Output :
Up => North
Down => South
Left => West
Right => East
Example 1: This example uses foreach loop to display the elements of associative array.
<?php // Declare an associative array $aso_arr = array( "Up"=>"North", "Down"=>"South", "Left"=>"West", "Right"=>"East"); // Use foreach loop to traverse each // elements of array and display its // key and value foreach($aso_arr as $side=>$direc) { echo $side . " => " . $direc . "\n"; } ?> |
Up => North Down => South Left => West Right => East
Example 2: This example uses array to display its index with value.
<?php // Declare an array $sides = array("Up", "Down", "Left", "Right"); // Use foreach loop to display the // elements of array foreach($sides as $index => $value) { echo "sides[" . $index . "] => " . $value . " \n"; } ?> |
sides[0] => Up sides[1] => Down sides[2] => Left sides[3] => Right
Note: Every entry of the indexed array is similar to an associative array in which key is the index number.
For example:
$sides = array("00"=>"Up", "01"=>"Down", "02"=>"Left", "03"=>"Right");
$directions = array("00"=>"North", "01"=>"South", "02"=>"West", "03"=>"East");
Since the index are common in all indexed arrays so it can use these indexes to access the value in other arrays.
Example 3:
<?php // Declare and initialize array $sides = array("Up", "Down", "Left", "Right"); $directions = array("North", "South", "West", "East"); // Use foreach loop to display array elements foreach( $sides as $index => $side ) { echo $side . " => " . $directions[$index] . " \n"; } ?> |
Up => North Down => South Left => West Right => East
Recommended Posts:
- How to loop through an associative array and get the key in PHP?
- How to remove an array element in a foreach loop?
- foreach() loop vs Stream foreach() vs Parallel Stream foreach()
- PHP | foreach Loop
- C# | Using foreach loop in arrays
- How to find the index of foreach loop in PHP ?
- Determine the first and last iteration in a foreach loop in PHP?
- How to remove a key and its value from an associative array in PHP ?
- How to check an array is associative or sequential in PHP?
- Convert an object to associative array in PHP
- How to get numeric index of associative array in PHP?
- How to get a list of associative array keys in JavaScript ?
- PHP program to add item at the beginning of associative array
- How to access an associative array by integer index in PHP?
- How to remove Objects from Associative Array in JavaScript ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



