SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2007
    Posts
    92
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Is this possible?

    Is this:
    PHP Code:
    // Gives error
    echo $yep[$mama[food]]; 
    possible to do some other way than this:
    PHP Code:
    // No error
    $food $mama[food];
    echo 
    $yep[$food]; 
    ?

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,192
    Mentioned
    17 Post(s)
    Tagged
    4 Thread(s)
    PHP Code:
    $yep = array('burger'=>2);
    $mama = array('mother'=>'burger');
    $food 'mother';

    echo 
    $yep[$mama[$food]]; 
    ?

    If your receiving an error your array(s) must be set up improperly or this isn't the way you would do what your trying to do. Post a dump of the array(s).

    or… is it as simple as you forgot the $ for the food variable?

  3. #3
    SitePoint Enthusiast
    Join Date
    Jan 2007
    Posts
    92
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, i didn't forgot.
    I use mysql and I have array what need's mysql value, like this:

    PHP Code:
    $array[chicken] = "I want this out";

    $a1 mysql_query("SELECT food FROM table WHERE id='1';"$connect); 
    $mysql mysql_fetch_array($a1);

    // this value is 'chicken'
    $food $mysql[food];

    // outputs 'I want this out'
    echo $array[$food]; 
    Is this somehow possible to make simplier like this:

    PHP Code:
    $array[chicken] = "I want this out";

    $a1 mysql_query("SELECT food FROM table WHERE id='1';"$connect); 
    $mysql mysql_fetch_array($a1);

    // Gives error. Should output 'I want this out'
    echo $array[$mysql[food]]; 

  4. #4
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    $aLookup 
    = array(
        
    'chicken'    =>    'Kentucky Fried'
    );
    $rResult mysql_query("SELECT food FROM table WHERE id = 1");
    $aRow mysql_fetch_assoc($rResult);
    echo 
    $aLookup[$aRow['food']]; #Kentucky Fried
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  5. #5
    SitePoint Enthusiast
    Join Date
    Jan 2007
    Posts
    92
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmm.. I didn't get it to work, but it does not matter if there is no simplier way. Thanks anyway!

  6. #6
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    When using arrays, you must put the key in quotes if it is a string.
    PHP Code:
    $array[key]; //BAD :(
    $array['key']; //good :) 
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona


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
  •