You are here: Re: Iterating Over DB Results (Idiom?) « PHP Programming Language « IT news, forums, messages
Re: Iterating Over DB Results (Idiom?)

Posted by Schraalhans Keukenmeester on 05/09/07 15:53

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.

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация