Coding Villa

Nested arrays in PHP

Author : nida khawar

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

Description


In this simple article you will learn that how we use nested arrays in PHP.


Advertisements


 

 

Arrays in php:

 

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

 

·         Numeric arrays

·         Associative array

·         Multidimensional array

 

 

Nested arrays:

 

Nested array is an array that contain array within array. Array with in array are called nested arrays. Php support nested arrays.

 

Syntax:

 

Array("A","B",array("smile","sad"),"c");

 

Nested array in PHP:

 

In the code below we have created array with in array that is nested array using php.  Array contain another array as an element of that array. That nested array is treated as an element of a array.

 

 

Code:

 

 

<?php

$locations = array("C","A",array("Boston","Des Moines"),"B");

   echo count($locations,1);

?>

 

 

Another example:

 

 

<?php

$array["sunil"] = "Bhatia";

$array[0] = array("one"=>"two",array(1,2,3,array(4,5,6)));

$array["sunil2"] = "Bhatia2";

 processArray($array);

 $count=0;

 function processArray($array) {

            global $count;

            $count++; //this variable is for calculating tab space

 

            if(is_array($array) === true)

foreach($array as $key => $value) {

                                    if(is_array($array[$key]) === true) {

                                                processArray($array[$key]);

                                    }

                                    else {

for($i = 1 ; $i < $count ; $i++) echo "&nbsp;&nbsp;";

echo $value."<br />";

                                    }

                        }

            }

            $count--;

}

?>

 

Above is a simple code for nested array in php.

 

This simple article tells that how we can nest arrays 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