At Wed, 09 May 2007 07:51:16 -0700, john let his monkeys type:
All:
I'm using MDB2 to pull data from a MySQL database... works fine, e.g.:
$result = getData(...);
Now that I have the $result (a multidimensional array), what is the
PHP idiom for iterating through the set, e.g., to create an HTML table
of the data? Should I be thinking foreach() or something else?
Thanks,
jpuopolo
An example of how to traverse through a multidimensional array using
foreach (recursively):
function foreach_multi_arr (&$arr) {
foreach ($arr as $key=>$value) {
echo "[$key]=>";
if (is_array($value)) {
foreach_multi_arr($value);
}
else {
echo " $value<br />";
// instead you could have a function acting on key/value pairs etc.
}
}
}
The '&' isn't required but if your array is huge the cost of copying each
subarray could become high. Similarly you may prepend $value
in the foreach with & to save on memory. (Only in PHP5)
HTH
Sh.