Jump to content

return a 2d array


MadnessRed

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/148973-return-a-2d-array/
Share on other sites

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;
}
}

Link to comment
https://forums.phpfreaks.com/topic/148973-return-a-2d-array/#findComment-782253
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.