PHP multidimensional array


The value in one array can be another array, and the value of another array can also be an array. In this way, we can create two-dimensional or three-dimensional arrays:

Instance

<?php
 // Two-dimensional array:
 $cars = array
 (
     Array("Volvo",100,96),
     Array("BMW",60,59),
     Array("Toyota",110,100)
 );
 ?>
PHP - Multidimensional array

A multidimensional array is an array of one or more arrays.

In a multidimensional array, each element in the main array can also be an array, and each element in the subarray can also be an array.

Instance

In this example, we created a multidimensional array that automatically assigns ID keys:

Instance

<?php
 $sites = array 
 ( 
     "php"=>array 
     ( 
         "php Chinese network", 
         "http://www.php.cn" 
     ), 
     "google"=>array 
     ( 
         "Google Search", 
         "http://www.google.com" 
     ), 
     "taobao"=>array 
     ( 
         "Taobao", 
         "http://www.taobao.com" 
     ) 
 ); 
 Print(""); // format the output array 
 Print_r($sites); 
 Print(""); 
 ?>
    
Let's try to display a value in the above array:

Echo $sites['php'][0] . 'Address is:' . $sites['php'][1];

The above code will output:

Multidimensional Arrays

A multidimensional array refers to an array containing one or more arrays.

1. There are no other arrays in the one-dimensional array array, only some variables or values.

2. Insert a single layer of an array into a two-dimensional array array, or multiple arrays

3. The three-dimensional array inserts an array (B) into the array (A), and inserts a level array (C) into the B array. This is called a three-dimensional array.

4. More than three-dimensional, all are called multi-dimensional arrays.

Difficulties in learning multidimensional arrays:

Pay attention to the format, and align the line breaks and indents of each dimension. It is not easy to make mistakes.

[Remember] 
The separator between array elements is a comma. When inserting an array into an array, do not write a semicolon (;) at the end.

Last Updated: 2018-07-06 12:32:44 By akshay

SHARE THIS

share_facebook share_twitter share_google_plus

Please Comment Here :