Making 2-dimensional arrays...slight problem [Solved]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
supergrover1981
Forum Newbie
Posts: 11
Joined: Sun Nov 26, 2006 6:36 am

Making 2-dimensional arrays...slight problem [Solved]

Post by supergrover1981 »

Gidday gang,

I'm trying to create a class that figures out how many columns a mysql query has, then makes an array with that number of columns. I'm new to PHP, but I imagine this is a fairly standard procedure. I've managed to make the array without any problems, but I'm having trouble returning the results.

Here's the difficulty:

Code: Select all

function returnarray($array) {
		$returnarray = '';
		for ($i=0; $i<(thenumberofrows); $i++) {
			foreach ($array as $key=>$value) {
				$returnarray .= $value[$i];
			}
		}
}
The problem I have is that I can't figure out how to calculate (thenumberofrows) for the FOR loop. I'm guessing it's some sort of 'count' function, but the only count functions I know only return the number of columns, not rows. Here's the code I used to assemble the array in the first place:

Code: Select all

function makearray($query) {
		$connect = mysql_query($query, $this->connection);
		if (!$connect)
		{
			die('Could not run the query');
		}
		else
		{
			$i = 0;
			while($row = mysql_fetch_assoc($connect)) {
				foreach ($row as $key=>$value) {
					$returnthis[$key][$i] = $value;
		 			
				}
			$i++;		
			}	
		}
	return $returnthis;
	}
If anyone has any suggestions, I'd be most appreciative. Apologies for the noob-ness of it all. :-p

Lotsa thanks,
- SG
Last edited by supergrover1981 on Wed Mar 21, 2007 6:27 pm, edited 1 time in total.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Making 2-dimensional arrays...slight problem

Post by Christopher »

The function you are looking for is count(), though I am not sure you need it. I think you just have your array indexes in the wrong order. The array indexes go from left to right from outermost array to innermost array.

Code: Select all

function makearray($query) {
		$allrows = array();      // usually good to initialize arrays
		$connect = mysql_query($query, $this->connection);
		if (!$connect)
		{
			die('Could not run the query');
		}
		else
		{
                        $i = 0;
			while($row = mysql_fetch_assoc($connect)) {
				foreach ($row as $key=>$value) {
					$allrows[$i][$key] = $value;     // this should work now
		 			
				}
			        $i++;		
			}	
		}
	return $allrows;
	}
(#10850)
supergrover1981
Forum Newbie
Posts: 11
Joined: Sun Nov 26, 2006 6:36 am

Post by supergrover1981 »

Ach! Of course. :-)

Many thanks Christopher, I really do appreciate it.

(Just in case anyone's interested, the returnarray function for this is:

Code: Select all

function returnarray($array, $divspecs, $enddiv) {
	$returnarray = '';
	for ($i=0; $i<count($array); $i++) {
		foreach ($array[$i] as $key=>$value) {
			$returnarray .= $divspecs.$value.$enddiv;
		}
	}
return $returnarray;
}
Thanks again,
- SG
Post Reply