Home >>Advance PHP Tutorial >PHP nested Array
array(array(val1, val1, val2..), array(val1,val2,val3..))
<?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; ?>
<?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; ?>
<?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>'; ?>
Name | Mobile |
---|---|
neeraj | 342353534 |
rohit | 34235 |
deepak | 33534 |
<?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>
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]; ?>