SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,223
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    variables inside variable names

    Code:
    $EuropeCountry=array("Germany");
    
    $Germany_continent="Europe";
    I have 2 variables like the above.

    The value of the first variable "$EuropeCountry[0]" is "Germany".
    The value of the second variable "$Germany_continent" is "Europe".

    I like to replace "Germany" in the second variable name "$Germany_continent" with the first variable name "$EuropeCountry[0]".

    The trial code below is for it.
    Code:
    trial code
    $EuropeCountry=array("Germany");
    
    $($EuropeCountry[0])_continent="Europe";
    
    result
    parse error

  2. #2
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dotJoon View Post
    PHP Code:
    $($EuropeCountry[0])_continent="Europe"
    that gives a parse error. Try a multidimensional array instead
    PHP Code:
    $world = array();
    $world[] = array('country'=>'Germany''continent'=>'Europe');
    $world[] = array('country'=>'USA''continent'=>'America');
    $world[] = array('country'=>'Japan''continent'=>'Asia');
    $world[] = array('country'=>'China''continent'=>'Asia'); 
    or a simpler associative array
    PHP Code:
    $world = array();
    $world['Germany'] = 'Europe';
    $world['USA'] = 'America';
    $world['Japan'] ='Asia';
    $world['China'] = 'Asia'
    Denny Schlesinger
    web services

  3. #3
    SitePoint Evangelist
    Join Date
    Oct 2005
    Location
    Michigan, USA
    Posts
    434
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    It does seem that maybe the structure of your code isn't the best idea, though I couldn't say for sure since I haven't really seen any of it. So you may want to take captainccs' thoughts and re-work your code some.
    But to answer your question, there is a concept called "variable variables"
    PHP Code:
    $EuropeCountry = array( 'Germany' );
    $var_name "{$EuropeCountry]}_continent"// $var_name contains the string "Germany_continent"
    ${$var_name} = 'Europe'// we're assigning a value to a variable named "Germany_continent"

    echo $Germany_continent// will print "Europe" 
    - Robert

  4. #4
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dotJoon View Post
    The trial code below is for it.
    Code:
    trial code
    $EuropeCountry=array("Germany");
    
    $($EuropeCountry[0])_continent="Europe";
    
    result
    parse error
    Here are a few examples of variable variables:

    PHP Code:
    ${$EuropeCountry[0] . '_continent'} = 'Europe';
    // or
    $variable_name $EuropeCountry[0] . '_continent';
    $
    $variable_name 'Europe'
    See http://php.net/variables.variable
    Salathe
    Software Developer and PHP Manual Author.


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
  •