Coding Villa

Iterate from multi-dimensional array using for each loop in PHP

Author : nida khawar

Share with Friends
Views : 351
Ratings : 0
Bookmarks : 0
Farourities : 0

Description


In this simple article you will learn that how we can iterate from multi-dimensional array using for each loop in PHP.


Advertisements


 

 

Arrays in php:

 

Arrays are just like the variable that store values but arrays can be used to store more than one value. Variable only store one value. There are three types of arrays

 

·         Numeric arrays

·         Associative array

·         Multidimensional array

 

Multidimensional arrays in php:

 

Multidimensional is a type of array that contains one or more arrays. In this type of array each element in the main array can also be an array & each element in the sub-array can be an array.

 

For each loop in php:

 

For each loop is used when we want to use every element of an array and we want to iterate through every element php provide us for each loop for this purpose.

 

Iterate multidimensional array using for each loop in php:

 

Multidimensional arrays contain more than one array so in order to use every element of array we use for each loop in php. For each is the simplest way to looping multidimensional arrays. For this we use two for each loop, outer loop is for the iteration of outer elements and inner loop for the iteration of the inner element. In the given example we have an associative multidimensional array we take two for each statement for the iteration of outer and inner elements.

 

 

Code:

 

<?php

$flavors = array('Japanese' => array('hot' => 'A',  'salty' => 'B'),  'Chinese'  => array('hot' => 'D', 'pepper-salty' => 'C'));

foreach ($flavors as $culture => $culture_flavors)

 {

    foreach ($culture_flavors as $flavor => $example)

{

        print "A $culture $flavor flavor is $example.\n";

    }

}

?>

 

Above is a simple code to iterate multidimensional array using for each loop in php.

 

This simple article tells that how we can iterate multidimensional arrays using for each loop in php. 

 



Article Tags
Share with Friends

Comments




Leave a Reply

Name (required)  
Mail (will not be published) (required)   
 
Enter This Code
Captcha
 

Advertisements

Article Categories

.NET


Databases


Mobile Development


Operating Systems


Web Development


Coding Villa on Facebook