Page 1 of 1

Help with Array

Posted: Wed Mar 15, 2006 12:53 pm
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

Posted: Wed Mar 15, 2006 1:21 pm
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.