Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
com
T HE W O R LD ' S LAR GE S T W E B D E VE LO P E R S IT E
HTML
SQL
JQUERY
CSS
PHP
ANGULAR
JAVASCRIPT
BOOTSTRAP
XML
TUTORIALS
REFERENCES
EXAMPLES
FORUM
Next Chapter
Earlier in this tutorial, we have described arrays that are a single list of key/value pairs.
However, sometimes you want to store values with more than one key.
This can be stored in multidimensional arrays.
pdfcrowd.com
Stock
Sold
Volvo
22
18
BMW
15
13
pdfcrowd.com
Saab
Land Rover
17
15
We can store the data from the table above in a two-dimensional array, like this:
$cars = array
(
array("Volvo",22,18),
array("BMW",15,13),
array("Saab",5,2),
array("Land Rover",17,15)
);
Now the two-dimensional $cars array contains four arrays, and it has two indices: row and column.
To get access to the elements of the $cars array we must point to the two indices (row and column):
Example
<?php
echo $cars[0][0].": In stock: ".$cars[0][1].", sold:
".$cars[0][2].".<br>";
echo $cars[1][0].": In stock: ".$cars[1][1].", sold:
open in browser PRO version
pdfcrowd.com
".$cars[1][2].".<br>";
echo $cars[2][0].": In stock: ".$cars[2][1].", sold:
".$cars[2][2].".<br>";
echo $cars[3][0].": In stock: ".$cars[3][1].", sold:
".$cars[3][2].".<br>";
?>
Run example
We can also put a For loop inside another For loop to get the elements of the $cars array (we still have
to point to the two indices):
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>";
}
open in browser PRO version
pdfcrowd.com
?>
Run example
Previous
Next Chapter
W3SCHOOLS EXAMS
HTML, CSS, JavaScript, PHP, jQuery, and XML Certifications
COLOR PICKER
pdfcrowd.com
LEARN MORE:
Color Converter
Google Maps
Animated Buttons
Modal Boxes
Modal Images
Tooltips
Loaders
JS Animations
Progress Bars
Dropdowns
Slideshow
Side Navigation
HTML Includes
Color Palettes
pdfcrowd.com
REPORT ERROR
PRINT PAGE
FORUM
ABOUT
Top 10 Tutorials
Top 10 References
HTML Tutorial
CSS Tutorial
JavaScript Tutorial
W3.CSS Tutorial
Bootstrap Tutorial
HTML Reference
CSS Reference
JavaScript Reference
W3.CSS Reference
Browser Statistics
SQL Tutorial
PHP Tutorial
jQuery Tutorial
PHP Reference
HTML Colors
HTML Character Sets
pdfcrowd.com
Angular Tutorial
XML Tutorial
jQuery Reference
AngularJS Reference
Top 10 Examples
Web Certificates
HTML Examples
CSS Examples
JavaScript Examples
W3.CSS Examples
HTML DOM Examples
PHP Examples
jQuery Examples
ASP Examples
XML Examples
SVG Examples
HTML Certificate
HTML5 Certificate
CSS Certificate
JavaScript Certificate
jQuery Certificate
PHP Certificate
Bootstrap Certificate
XML Certificate
W3Schools is optimized for learning, testing, and training. Examples might be simplified to improve reading and basic
understanding. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full
correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy
policy. Copyright 1999-2016 by Refsnes Data. All Rights Reserved.
pdfcrowd.com