The values in one array can be another array, and the values in another array can also be an array. In this way, we can create a two-dimensional or three-dimensional array:
Example
<?php // 二维数组: $cars = array ( array("Volvo",100,96), array("BMW",60,59), array("Toyota",110,100) ); ?>
PHP - Multidimensional Array
Multidimensional array is An array containing one or more arrays.
In a multi-dimensional array, each element in the main array can also be an array, and each element in the sub-array can also be an array.
In this example, we create a multidimensional array with automatically assigned ID keys:
<?php $sites = array ( "runoob"=>array ( "php中文网", "http://www.php.cn" ), "google"=>array ( "Google 搜索", "http://www.google.com" ), "taobao"=>array ( "淘宝", "http://www.taobao.com" ) ); print("<pre class="brush:php;toolbar:false">"); // 格式化输出数组 print_r($sites); print("
The above array will output the following:
Example 2
Let us try to display a value in the above array:
echo $sites ['runoob'][0] . 'The address is:' . $sites['runoob'][1];
Methods for multi-dimensional array traversal
$a=array('fruits'=>array('a'=>'orange','b'=>'grape',c=>'apple'), 'numbers'=>array(1,2,3,4,5,6), 'holes'=>array('first',5=>'second','third') ); //第一种: foreach($a as $list=>$things){ if(is_array($things)){ foreach($things as $newlist=>$counter){ echo "key:".$newlist."<br/>"."value:".$counter."<br/>"; } } } //第二种: function MulitarraytoSingle($array){ $temp=array(); if(is_array($array)){ foreach ($array as $key=>$value ) { if(is_array($value)){ MulitarraytoSingle($value); } else{ $temp[]=$value; } } } }
The above is the detailed content of Deep talk on php multidimensional arrays. For more information, please follow other related articles on the PHP Chinese website!