Wednesday, April 12, 2017

Multi-dimension array example

<?php
$menu=array("food" => array("continental"=>"bread", "chinese"=>"noodles"),
            "languages"=> array("functional"=>"scala", "script"=>"perl", "web"=>"php"));
foreach($menu as $key=>$value) {
 echo "$key "."=>"."$value <br/>";
 echo "___________________________<br/>";
 echo "foreach starts <br/>";
 echo "___________________________<br/>";
 foreach($value as $key1=>$value1) {
  echo "$key1 "."=>"."$value1 <br/>";
 }
 echo "___________________________<br/>";
 echo "foreach ends<br/>"; 
 echo "___________________________<br/>";
}
echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><br/>";
foreach($menu as $key=>$value) {
 echo "$key "."=>"."$value <br/>";
 echo "___________________________<br/>";
 echo "each(\$value):". each($value) . "<br/>";
 echo "___________________________<br/>";
 while(list($key1, $value1)=each($value)) {
  echo $key1 . "=" . $value1 . "<br/>";
 }
 echo "___________________________<br/>";
}
echo "Continental food: ".$menu['food']['continental']; 
echo var_dump($menu);
?>
Output:
food =>Array 
___________________________
foreach starts 
___________________________
continental =>bread 
chinese =>noodles 
___________________________
foreach ends
___________________________
languages =>Array 
___________________________
foreach starts 
___________________________
functional =>scala 
script =>perl 
web =>php 
___________________________
foreach ends
___________________________
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
food =>Array 
___________________________
each($value):Array
___________________________
chinese=noodles
___________________________
languages =>Array 
___________________________
each($value):Array
___________________________
script=perl
web=php
___________________________
Continental food: breadarray(2) { ["food"]=> array(2) { ["continental"]=> string(5) "bread" ["chinese"]=> string(7) "noodles" } ["languages"]=> array(3) { ["functional"]=> string(5) "scala" ["script"]=> string(4) "perl" ["web"]=> string(3) "php" } }

No comments:

Post a Comment

here docs

$x = <<<THE_END PHP stands for "PHP: Hypertext Preprocessor". The acronym "PHP" is therefore, usually referre...