简体   繁体   中英

Indexed two-dimensional array

If you have an indexed two-dimensional array named $StateCapitals, and this arrays index values start with 0, which of the following refers to the second element in the first dimension and the third element in the second dimension?

$StateCapitals [1] [2]      
$StateCapitals [1], [2]     
$StateCapitals [2] [3]      
$StateCapitals [2], [3]

Arrays start counting from 0, so the count goes: 0 (first), 1 (second), 2 (third). Next, this is an issue of syntax. The , operator does something very different in PHP (and most languages) then handle this. You would use:

$StateCapitals [1] [2] 

Example to try:

<?php

$temp = Array (
    Array(2,3,4),
    Array(4,5,6),
);

echo $temp [1] [2];

?>

Strictly speaking the "second element in the first dimension" is simply the second array of the outer array. And the "third element in the second dimension" would depend on the index of the array you selected from the first.

But judging from the examples you gave I think your array might look like this.

$StateCapitals = array(
    0=>array(0=>'value a',1=>'value b',2=>'value c'),
    1=>array(0=>'value a',1=>'value b',2=>'value c'),
    2=>array(0=>'value a',1=>'value b',2=>'value c')
);

If so then the "second element in the first dimension." would be:

$StateCapitals[0][1];

"the third element in the second dimension" would be:

$StateCapitals[1][2];

It depends on how loosely you interpret the language you used.

hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM