PHP-Multidimensional arrays

Source: Internet
Author: User

A multidimensional array refers to an array that contains one or more arrays.

PHP can understand multi-dimensional arrays of two or three, four, or five levels or more. However, arrays that are more than three levels deep are difficult for most people to manage.

Note: The dimensions of an array indicate the number of indexes you need to select an element.

    • For a two-dimensional array, you need two indexes to select the element
    • For three-dimensional arrays, you need three indexes to select the elements
PHP-two-D arrays

A two-d array is an array of arrays (three-dimensional arrays are arrays of arrays).

First, let's take a look at the following table:

We can store the data in the table in a two-D array, like this:

$cars =Array (Array ("Volvo", A, -), Array ("BMW", the, -), Array ("Saab",5,2), Array ("Land Rover", -, the)  );

Now this two-dimensional array contains four arrays, and it has two indexes (subscript): Rows and columns.

To access the elements in the $cars array, we must use two indexes (rows and columns):

Example
<?Phpecho $cars [0][0].": Stock:". $cars [0][1].", Sales:". $cars [0][2].".<br>"; Echo $cars [1][0].": Stock:". $cars [1][1].", Sales:". $cars [1][2].".<br>"; Echo $cars [2][0].": Stock:". $cars [2][1].", Sales:". $cars [2][2].".<br>"; Echo $cars [3][0].": Stock:". $cars [3][1].", Sales:". $cars [3][2].".<br>";?>

We can also use another for loop in the for loop to get the elements in the $cars array (we still need to use two indexes):

Example
<?PHP for($row =0; $row <4; $row + +) {echo"<p><b>row number $row </b></p>"; Echo"<ul>";  for($col =0; $col <3; $col + +) {echo"<li>". $cars [$row] [$col]."</li>"; } Echo"</ul>";}?>

PHP-Multidimensional arrays

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.