Help with Array

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
User avatar
kurupt4
Forum Newbie
Posts: 10
Joined: Tue Dec 17, 2002 2:27 pm
Location: Tustin, CA

Help with Array

Post by kurupt4 »

Hi all,

I don't know why this is giving me such a problem but it is.

I have 2 sets of values that i save to an MySQL database, they are seperated by commas as such:

Set 1: "San Jose,Tustin,Orange,Santa Ana,"
Set 2: "10,13,2,4"

The set 2 values correspond to the set 1 labels

Now i pull set 1 of values out of the database, I split the values using the split() function and assign them to an array
and I do the same with set 2.

Now what I am having problems with is: I want to assign the value 10 to San Jose, 13 to Tustin and so forth in one array that I can then do an asort on.

I use the following code but for some reason I only get one pair outputted??

Any and all help will be very much appreciated:

Code: Select all

//Now split up the choices
$choicelist = split(",", $choices, -1);

//Now split up the vote
$votelist = split(",", $votetally, -1);

for ($count = 0; $count < 3; $count++)
{
	$totalline = array('Votes' => $votelist[$count], 'Choice' => $choicelist[$count]);
}

arsort($totalline);

while ($totalline as $key => $value)
	echo $key . ' ' . $value . '<br />';
When I run it I get this output:

Votes 10
Choice San Jose

and that's all.

What am I missing or doing wrong?

Thank you all in advance

Leo Zayas
PoliceSoftball.com
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I'd suggest using explode() over split(), as your strings do not require regex (especially the slow posix ones)

Anyways, you can use array_combine() (there's a PHP 4 implementation given on the page) to combine them into a single associative array.
Post Reply