Results 1 to 7 of 7

Thread: Interesting array problem

  1. #1
    Join Date
    Apr 2009
    Location
    Cognac, France
    Posts
    400
    Thanks
    2
    Thanked 57 Times in 57 Posts

    Default Interesting array problem

    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

    PHP Code:
    $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
    Last edited by forum_amnesiac; 05-17-2009 at 06:36 PM.

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    That's because you don't make an array like this:
    Code:
    $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.
    Jeremy | jfein.net

  3. #3
    Join Date
    Apr 2009
    Location
    Cognac, France
    Posts
    400
    Thanks
    2
    Thanked 57 Times in 57 Posts

    Default

    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 :-
    PHP Code:
    $num=mysql_num_rows($result);
    $x=0;
    while (
    $x $num) {

        
    $catidmysql_result($result,$x,"cat_id");
        
    $catgorymysql_result($result,$x,"category");
        
    $cat=$cat.'"'.$catid.'"=>"'.$catgory.'", ';    
    $x++; 
    I had originally tried building $cat like this
    PHP Code:
    $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.

  4. #4
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    If you just want to make an array with all the results, use the [] symbols. They'll add to the array. Try maybe this:
    Code:
    $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?
    Jeremy | jfein.net

  5. #5
    Join Date
    Apr 2009
    Location
    Cognac, France
    Posts
    400
    Thanks
    2
    Thanked 57 Times in 57 Posts

    Default

    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.

    PHP Code:
    $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:

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

        
    $catidtrim(mysql_result($result,$x,"code"));
        
    $catgorytrim(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.

  6. #6
    Join Date
    Apr 2009
    Location
    Cognac, France
    Posts
    400
    Thanks
    2
    Thanked 57 Times in 57 Posts

    Default

    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.

  7. #7
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    Quote Originally Posted by forum_amnesiac View Post
    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •