Home >>Advance PHP Tutorial >PHP nested Array

PHP nested Array



PHP nested array

Syntax
	array(array(val1, val1, val2..), array(val1,val2,val3..))

Create a two dimensional numeric array and find the sum.

<?php

error_reporting(1);

$arr=array(array(10,10,10),array(10,10,10),array(10,10,10));

$s=0;

$s1=0;
//using for loop:

for($i=0;$i<3;$i++)

{

	for($j=0;$j<3;$j++)

	{

	echo $arr[$i][$j]." ";

	$s=$s+$arr[$i][$j];

	}

	echo "<br>";

}

echo "sum of array:".$s;
 
?>

Output 10 10 10 10 10 10 10 10 10 sum of array : 90
In the above example Create a variable $arr with value an nested array. Here at the place of array's first element there is also an array with three value like $arr[0][0]=10, $arr[0][1]=10, and $arr[0][2]=10. at the second index the value are : $arr[1][0]=10, $arr[1][1]=10, and $arr[1][2]=10. at the 3rd index the value are : $arr[2][0]=10, $arr[2][1]=10, and $arr[2][2]=10. Now first call all nested array using for loop. first for loop call the three array(row) one by one, inside this loop again call a for loop i.e used to call first array's all value(columns of row), print it using echo statement and also make sum ans store inside a variable $s. So the output will display: first row's three column i.e 10 10 10 second row's three column i.e 10 10 10 Third row's three column i.e 10 10 10 and the final sum is 90

Create a two dimensional numeric array and find the sum using for-each

<?php

error_reporting(1);

$arr=array(array(10,10,10),array(10,10,10),array(10,10,10));

$s=0;

$s1=0;

//using foreach loop
foreach($arr as $k)

{

  foreach($k as $v)

   {

   echo $v;

   $s1=$s1+$v;

   }

 echo "<br>";

}

echo "sum of array:".$s1;

?>

Output 10 10 10 10 10 10 10 10 10 sum of array : 90

Two dimensional associative array( user's name and mobile number)

<?php

error_reporting(1);

$arr=array(array("name"=>"neeraj","mob"=>342353534),
           array("name"=>"rohit","mob"=>34235),
           array("name"=>"deepak","mob"=>33534)
          );

echo '<table border="2">';

echo '<tr>';

echo '<td align="center">Name:</td>';

echo '<td align="center">MOb:</td>';

foreach($arr as $k)

{

echo '<tr>';

foreach($k as $v)

{

echo '<td align="center">'.$v.'</td>';

}

echo '</tr>';

}

echo '</table>';

?>

Output
Name Mobile
neeraj 342353534
rohit 34235
deepak 33534

Two dimensional associative array

Display city according to selected country
<?php

 $country=array("ind"=>array("Lucknow","Rajasthan","Delhi"),
            "pak"=>array("Islamabad","Lahore"),
            "ch"=>array("ch1","ch2")
           );

if(isset($_GET['display']))

 {

 $get_country=$_GET['c'];

echo "City ";

 foreach($country as $country_key => $cname)

   {

     if($country_key==$get_country)

	   {

         echo "<select>";

       foreach($cname as $state)

         {
	 echo "<option>".$state."</option>";

	  }

	 echo "</select>"; 

           }

      }

 }

?>

<form method="get">

<select name="c">

<option value="ind">india</option>

<option value="pak">Pak</option>

<option value="ch">china</option>

</select>

<input type="submit" value="submit" name="display"/>

</form>

Output select country City
In the above example first create an array in which multiple index(country) are defined as array, on that array multiple city defined(as column of row). Now user has to select his country name from select box. according to selected country, find the city i.e stored on country index using foreach loop.

Multidimensional associative array

Syntax
	array(array(array(array(val1, val1, val2....))))
<?php
    $country=array("pak"=>"pakistan","ind"=>array("br"=>"bihar",
   "dl"=>array("Nd"=>"North delhi","sd"=>"south delhi",
   "Ed"=>array("dwarka","uttam nagar"))));

echo $country["ind"]["br"]."<br/>";

echo $country["ind"]["dl"]["Nr"]."<br/>";

echo $country["ind"]["dl"]["Ed"][0];
 
?>

Output bihar North delhi dwarka