简体   繁体   中英

PHP Array within an Array

  $row[];  // Declare array. PRETEND ITS AN ARRAY
  $row2[]; // Declare another
  $row3[]; // Declare one more

  $rowarray[];

  $rowarray[0] = $row
  $rowarray[1] = $row2
  $rowarray[2] = $row3 // Store array in an array

My Questions: 1. Is this valid or even useful? 2. If I do this, how do I access $row[0] $row[1] etc.

  1. The concept is valid, but the syntax is not -- arrays are not explicitly declared like that in PHP. A correct way to initialize this would be something like:

     $row1 = array(1, 2, 3); $row2 = array(4, 5, 6); $row3 = array(7, 8, 9); $rowarray = array($row1, $row2, $row3); 

    Or, equivalently and more succinctly:

     $rowarray = array( array(1, 2, 3), array(4, 5, 6), array(7, 8, 9) ); 
  2. $rowarray[1][2] . Indexes are in order, so, given the example data I used, this would be 6 (element 2 of the array which is element 1 of $rowarray ).

What you're looking for is a Multi-dimensional array.

For example:

$a1 = array(
   array(1, 2, 3, 4),
   array(1, 2, 3, 4)
);

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