PHP multidimensional array

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 two- or three-dimensional arrays:

Example

<?php
// 二维数组:
$cars = array
(
    array("Volvo",100,96),
    array("BMW",60,59),
    array("Toyota",110,100)
);
?>


Run Example»

PHP - Multidimensional Array

A multidimensional array is an array that contains 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.

Example

In this example, we create a multi-dimensional array with automatically assigned ID keys:

Example

<?php 
$sites = array 
( 
    "php"=>array 
    ( 
        "php中文网", 
        "http://www.php.cn" 
    ), 
    "google"=>array 
    ( 
        "Google 搜索", 
        "http://www.google.com" 
    ), 
    "taobao"=>array 
    ( 
        "淘宝", 
        "http://www.taobao.com" 
    ) 
); 
print("<pre>"); // 格式化输出数组 
print_r($sites); 
print("</pre>"); 
?>

The above array will be output as follows :

Array
(
[php] => http://www.php.cn
          )

                                                                                                                                    ‐ ‐ [1] ‐ ‐ ‐ ‐ ‐ [1] => http://www.google.com
)
## [TaoBao] = & GT; Array
(
[0] = & GT; Taobao
[1] = & gtp; http ://www.taobao.com
)

)





##Example 2
Let’s Try to display a value in the above array:

echo $sites['php'][0] . 'The address is:' . $sites['php'][1];

The above code will output:

PHP Chinese website address is: http://www.php.cn

Example 3

<?php
$all = array("fruits" => array("a"=>"orange","b"=>"banana","c"=>"apple"),                                    
         "ages" => array(18, 20, 25)                                  
      );    
    echo $all["fruits"]["c"];   //输出apple    
     echo $all["ages"][0];       //输出18
?>

Continuing Learning
||
<?php // 二维数组: $cars = array ( array("Volvo",100,96), array("BMW",60,59), array("Toyota",110,100) ); ?>
submitReset Code