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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.