Re: Multidimensional arrays
| From: | Rasmus Lerdorf | Date: | Mon, 19 Aug 2002 16:15:13 +0000 |
| Subject: | Re: Multidimensional arrays | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-113222@lists.php.net to get a copy of this message | ||
foreach($array as $i=>$values) {
echo $values['id'],$values['name']
}
On Mon, 19 Aug 2002, Richard Fox wrote:
> Hi,
>
> Given the array $array initialized by:
>
> $query = "SELECT id,name FROM MYTABLE";
> $result = mysql_query($query);
> $nrows = mysql_num_rows($result);
> for ($i=0; $i<$nrows; $i++) {
> $arr = mysql_fetch_array($result);
> $array[$i]['id'] = $arr['id'];
> $array[$i]['name'] = $arr['name'];
> }
>
> I know I can have a similar 'for' loop to step through the array, from 0 to
> count($array)
> But I would like to do something more elegant, with a foreach loop. But I have been
> unsuccessful as a foreach seems appropriate only with a single dimensional array. Can someone
> suggest a more elegant solution? Thanks, Rich
> P.S. yes, I am fairly new to PHP, less than 6 months. Probably my array loading method could be
> improved also!
>
>
>