Create an associative array from table

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
csdavis0906
Forum Newbie
Posts: 17
Joined: Thu Mar 27, 2008 9:36 am

Create an associative array from table

Post by csdavis0906 »

I am trying to create an associative array reading fields tag & count from a table named tags.

Doing it manually, I would use:

Code: Select all

/* array of items and sizes
$tagArray = array(
	"Apple" => 9,
	"Cell Phone" => 12,
	"Computer" => 6,
	"eBay" => 1,
	"Gadget" => 3
);
I have tried looping through the table as follows:

Code: Select all

$query = mysql_query("SELECT * FROM tags ORDER BY count DESC");

// loop through table world counting occurrences of parent
while ($row = mysql_fetch_array($query)) {
	
   $tagArray [$tag] = $count;  // tried this and several other variations with no success
	
}
Any assistance would be greatly appreciated - this is driving me crazy!!! Thanking you in advance...
csdavis0906
Forum Newbie
Posts: 17
Joined: Thu Mar 27, 2008 9:36 am

Re: Create an associative array from table

Post by csdavis0906 »

I finally figured it out - I just added the param. MYSQL_ASSOC to the mysql_fetch_array statement and then
used: $tagArray[$parent] = $count;

Finally, yikes!!!!
dbemowsk
Forum Commoner
Posts: 82
Joined: Wed May 14, 2008 10:30 pm

Re: Create an associative array from table

Post by dbemowsk »

It is difficult to understand what you are trying to do. In your first post, where is the variable $tag and $count coming from? Likewise, in your second post, where is $parent coming from?
Last edited by dbemowsk on Sun May 18, 2008 6:27 am, edited 1 time in total.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Create an associative array from table

Post by VladSun »

mysql_fetch_assoc()
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply