MadnessRed Posted March 11, 2009 Share Posted March 11, 2009 Hi, I would like to know how to return a 2d array form a table. eg array( [0] => array('id' => 1, 'name' => 'User 1', pass => 'md5 string'), [1] => array('id' => 2, 'name' => 'User 2', pass => 'md5 string'), [3] => array('id' => 3, 'name' => 'User 3', pass => 'md5 string') ) All I want to know is if there is a function that already does this, if not then I can make one with relative easy but theres no point re-inventing the wheel. Quote Link to comment https://forums.phpfreaks.com/topic/148973-return-a-2d-array/ Share on other sites More sharing options...
Mchl Posted March 11, 2009 Share Posted March 11, 2009 http://www.php.net/manual/en/mysqli-result.fetch-all.php Quote Link to comment https://forums.phpfreaks.com/topic/148973-return-a-2d-array/#findComment-782213 Share on other sites More sharing options...
MadnessRed Posted March 11, 2009 Author Share Posted March 11, 2009 looks great but I don't have mysqli, and its not my server so I can't install it. Any other methods? Quote Link to comment https://forums.phpfreaks.com/topic/148973-return-a-2d-array/#findComment-782232 Share on other sites More sharing options...
MadnessRed Posted March 11, 2009 Author Share Posted March 11, 2009 ok, well i wrote my own function. If there is a better way 'd be interested to know. The advantage in this is that it will also return a 1d array here appropriate. function FetchAll($results, $rowsarray = true) { $rows = mysql_num_rows($results); if($rows > 1){ $return = array(); while ($row = mysql_fetch_array($results)){ if($rowsarray){ if(sizeof($row) > 2){ //Its two because we have an assiative array so both 0 and `id` for example of the same. $return[] = $row; }else{ $return[] = $row[0]; } }else{ $return[] = $row; } } return $return; }elseif($rows == 1){ return mysql_fetch_array($results,$type); }else{ return false; } } Quote Link to comment https://forums.phpfreaks.com/topic/148973-return-a-2d-array/#findComment-782253 Share on other sites More sharing options...
Mchl Posted March 11, 2009 Share Posted March 11, 2009 Looks good. Probably the best you can do without mysqli. Quote Link to comment https://forums.phpfreaks.com/topic/148973-return-a-2d-array/#findComment-782263 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.