RE: [PHP] 2D array
| From: | Nold, Mark | Date: | Wed, 13 Dec 2000 02:16:01 +0000 |
| Subject: | RE: [PHP] 2D array | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-30002@lists.php.net to get a copy of this message | ||
----------------------------------------------------------------------------
-----------------
Disclaimer: The information contained in this email is intended only for the
use of the person(s) to whom it is addressed and may be confidential or
contain legally privileged information. If you are not the intended
recipient you are hereby notified that any perusal, use, distribution,
copying or disclosure is strictly prohibited. If you have received this
email in error please immediately advise us by return email at
postmaster@normandy.com.au and delete the email document without making a
copy.
----------------------------------------------------------------------------
-----------------
I think one of the points behind PHP is that you dont need to initialise an
array. I tend to find simply doing a test to see if the array exists and/or
is infact an array is usually sufficient (rather than creating it
uneccessarily.
But of course you may want to initial the arrays due to warning levels etc.
The best way i find to access a 2D array is as follows:
<?
$myary[1]["foo"] = 12345;
$myary[1]["bar"] = 23456;
$myary[1]["two"] = 34567;
$myary[1]["mew"] = 45678;
$myary[2]["foo"] = "aaaa";
$myary[2]["bar"] = "aaab";
$myary[2]["two"] = "aabc";
$myary[2]["mew"] = "abcd";
$myary[3]["two"] = 1234;
$myary[3]["bar"] = "of types";
$myary[3]["foo"] = "A mix";
$myary[3]["mew"] = array(1,2,3,4);
function make_table($ary){
if(is_array($ary)){
reset($ary);
$table = "<table border=1>\n";
$table .= "<tr>";
/* Build up a list of columns and create the heading row
* This is important otherwise misordered assigments
* end up in the wrong column
*
*/
$row = current($ary);
while(list($column,$value) = each($row)){
$table .= "<th>$column</th>";
$columns[] = $column;
}
$table .= "</tr>\n";
/* Now while through the 1st dimension
* and while through the list of columns
* to grab the 2nd dimension.
*
* If you weren't building a table you could
* just while through the $row instead of $columns
*
*/
reset($ary);
while(list($key,$row) = each($ary)){
$table .= "<tr>";
reset($columns);
while(list($no,$column) = each($columns)){
$value = $row[$column];
$table .= "<td>$value</td>";
}
$table .= "</tr>\n";
}
$table .= "</table>\n";
}
return $table;
}
// Now echo out the finished product where required.
echo make_table($myary);
?>
I find the $ary["key"]["column"] notation much easier to access.
PS: If you want to print tables you'd be best creating a set of functions
something similiar to the above so that you can turn any 2d array into a
table.
mn
Mark Nold
markn@enspace.com <mailto:markn@enspace.com>
Senior Consultant
Change is inevitable, except from vending machines.
-----Original Message-----
From: Soeren Staun-Pedersen [mailto:staun@itcampus.dk]
Sent: Tuesday, December 12, 2000 8:44 PM
To: Georges da Mata Claessen
Cc: php-general@lists.php.net
Subject: Re: [PHP] 2D array
> Thanks, but supposing I have only the number of elements the array must
> have?
To my best knowledge you cannot initialize an array of some specific size.
Fill it with rubbish values which you know what are (like decide that
-100000 means that the position is not taken).
Regards,
Soeren Staun-Pedersen - staun@netwarriors.org
------
"The internet is full, beat it"
- Me.