By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,967 Members | 1,684 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,967 IT Pros & Developers. It's quick & easy.

Iterating Over DB Results (Idiom?)

P: n/a
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

May 9 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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.
May 9 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.