Can PHP Foreach traverse 3D arrays
You can traverse. In PHP, you can iterate through a 3D array by nesting three layers of foreach statements with the syntax “foreach($array as $k1=>$v1){foreach($v1 as $K2 =>$v2){foreach($v2 as $K3 =>$v3){… }}} “. The foreach statement moves the pointer inside the array forward each time it loops through the array until it reaches the end of the array, stops the loop, and exits the loop.
<!--?</span--> php
header("Content-type:text/html; charset=utf-8");
$array = array(
'Anhui' => array(
'Hefei '=>array(' Shushan ',' Changfeng ',' Feidong '),
'Suzhou '=>array(' Yongqiao District ',' Lingbi County ',' Si County ')
)
);
var_dump($array);
foreach($array as $k1=>$v1){
Echo "province name: ".$k1."
";
foreach($v1 as $k2=>$v2){
Echo "
City name: ".$k2."< BR >< BR >";
foreach($v2 as $k3=>$v3){
Echo "range name: ".$v3."
";
}
}
}
? >