Log in

View Full Version : Resolved Interesting array problem



forum_amnesiac
05-15-2009, 12:14 PM
I'm trying to create an associative by reading from a table.

I can create the variable to make the array but for some reason it is not working, here is an example


$cat ='"8"=>"Antipasti", "17"=>"Capperi Medi", "1"=>"Carciofi", "2"=>"Cipolle", "3"=>"Funghi", "18"=>"Lupini", "4"=>"Melanzane", "16"=>"Olive Nere", "15"=>"Olive Verdi", "13"=>"Pate", "5"=>"Peperoni", "14"=>"Pesto", "6"=>"Pomodori", "12"=>"Sughi Biologici", "10"=>"Sughi Economici", "11"=>"Sughi Extra", "9"=>"Verdure", "7"=>"Zucchine"';

$cat1 = array($cat);

$cat2=array("8"=>"Antipasti", "17"=>"Capperi Medi", "1"=>"Carciofi", "2"=>"Cipolle", "3"=>"Funghi", "18"=>"Lupini", "4"=>"Melanzane", "16"=>"Olive Nere", "15"=>"Olive Verdi", "13"=>"Pate", "5"=>"Peperoni", "14"=>"Pesto", "6"=>"Pomodori", "12"=>"Sughi Biologici", "10"=>"Sughi Economici", "11"=>"Sughi Extra", "9"=>"Verdure", "7"=>"Zucchine",);

echo $cat1["2"];
echo $cat2["2"];

I have created variable $cat here with the contents hardcoded, normally this is created from the table.

However even in this example $cat1 returns nothing, $cat2 returns 'Cipolle' as I would expect.

Can somebody explain why it doesn't work as '$cat1 = array($cat);' and tell me how I should do it.

Thanks

Nile
05-15-2009, 12:46 PM
That's because you don't make an array like this:


$cat = array("'dog'=>'pet','tiger'=>'animal'");

When you use an array function on a string, it thinks that it's going to be a string inside an array, and puts quotes.

forum_amnesiac
05-15-2009, 01:47 PM
Can you tell me then how I can build the array so that I can end up with the same result as in $cat2 above.

This is my code that loops through and builds the string :-

$num=mysql_num_rows($result);
$x=0;
while ($x < $num) {

$catid= mysql_result($result,$x,"cat_id");
$catgory= mysql_result($result,$x,"category");
$cat=$cat.'"'.$catid.'"=>"'.$catgory.'", ';
$x++;

I had originally tried building $cat like this
$cat= array($cat.'"'.$catid.'"=>"'.$catgory.'", ');, but that again didn't work.

It should be possible to build this array but I've got a block on it.

Nile
05-15-2009, 01:55 PM
If you just want to make an array with all the results, use the [] symbols. They'll add to the array. Try maybe this:


$num=mysql_num_rows($result);
$x=0;
$arr = array();
while ($x < $num) {

$catid= mysql_result($result,$x,"cat_id");
$catgory= mysql_result($result,$x,"category");
//$cat=$cat.'"'.$catid.'"=>"'.$catgory.'", ';
$arr[$catid] = $category;
$x++;


Or something like that, do you get what I mean though?

forum_amnesiac
05-15-2009, 04:16 PM
Thanks for the info, I said I had a block.

Now for the interesting part.

I found some very complicated code that uses the array continents to populate a drop down list.

When I build the array like this the script works and creates the drop down.


$continents=array(
""=>"Select continent",
"na"=>"North America",
"eu"=>"Europe",
"sa"=>"South America",
"as"=>"Asia",
"oc"=>"Oceania",
"tt"=>"Test"
);

echo $continents[eu];

The echo returns Europe.

I created a table in MYSQL with 2 fields, code and name, and populated with the same data as above.

I then use this code to build the array:


$continents=array();
$result=mysql_db_query($database,"select * from continents order by `continent`");
$num=mysql_num_rows($result);
$x=0;
while ($x < $num) {

$catid= trim(mysql_result($result,$x,"code"));
$catgory= trim(mysql_result($result,$x,"continent"));
$continents[$catid] = $catgory;
$x++;
}
echo $continents[eu];

The echo also returns Europe.

The problem is that the script does not create the drop down list.

Can anyone tell me why these 2 arrays would be treated differently.

The script passes the array to a function that for some reason does not think that the version created from the table is populated.

forum_amnesiac
05-17-2009, 06:40 PM
Sorted

Did a print_r of both arrays.

The array created from the table needed to have an entry with a null reference.

When I declare the array I now set it like this, $continents=array(""=>"Select continent");, and everything works fine.

boogyman
05-18-2009, 06:09 PM
Sorted

Did a print_r of both arrays.

The array created from the table needed to have an entry with a null reference.

When I declare the array I now set it like this, $continents=array(""=>"Select continent");, and everything works fine.

Rather then assigning the NULL reference, I would suggest in your processing, that you check for proper values and throw an error for any other value given.