RE: [PHP] Multidimensional arrays

From: Date: Mon, 19 Aug 2002 16:18:19 +0000
Subject: RE: [PHP] Multidimensional arrays
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-113221@lists.php.net to get a copy of this message
[snip] $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 [/snip] That is kind of an ambiguos question there Rich. What needs to be more elegant, the method? the output? You could use a foreach key/value pairing if your values are such. See http://www.php.net/manual/en/control-structures.foreach.php HTH! Jay If you must choose between two evils, pick the one you’ve never tried before! *********************************************************** * Texas PHP Developers Conf Spring 2003 * * T Bar M Resort & Conference Center * * New Braunfels, Texas * * San Antonio Area PHP Developers Group * * Interested? Contact jay.blanchard@niicommunications.com * ***********************************************************

« previous php.general (#113221) next »