Combine few variable together...

Discussion in 'PHP' started by EagerWolf, Jan 14, 2007.

  1. #1
    Ok ... I will show example:

    $var1 = 'test';
    $var2 = '1';
    $var3 = '2';

    Those 3 vars should be combined to get smth like this:

    $together = test1['2'];

    This is used in foreach to generate a proper form. It is a call for array.
    foreach ($together as $key => $value)...



    I made it this way:
    $together = $var1.$var2[$var3];

    It didn't work in call for array...
    I've tried if all vars are properly set:
    print $var1.$var2.'['.var3.']'

    Result: test1['2']...
     
    EagerWolf, Jan 14, 2007 IP
  2. solidphp

    solidphp Peon

    Messages:
    46
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Here's one way to do it:

    
    $var1='test';
    $var2=1;
    $var3=2;
    
    $together=array($var1, $var2, $var3);
    foreach ($together as $key => $value)
    	{
    	echo "{$key} = {$value} <br />";
    	}
    
    PHP:
     
    solidphp, Jan 14, 2007 IP
  3. solidphp

    solidphp Peon

    Messages:
    46
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I wanted to post this way too:

    $together=array();
    $together['var1']='test 1';
    $together['var2']='test 2';
    $together['var3']='test 3';
    
    foreach ($together as $key => $value)
    	{
    	echo "{$key} = {$value} <br />";
    	}
    PHP:
    Does that help?
     
    solidphp, Jan 14, 2007 IP
  4. EagerWolf

    EagerWolf Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    No no ... the result of this example would be array...

    What I want to do is that this string would call a specific location of array ... First var (var1) is first part of array's name, second var (var2) is second part of array's name and var3 is number that calls part of this array.

    array.php
    <?php
    $test1 = array
    (
    1 => 'smth1',
    2 => 'smth2'
    );
    $test2 = array
    (
    1 => 'smthelse1',
    2 => 'smthelse2'
    );
    ?>


    myscript.php

    <?php
    include('array.php');
    $var1 = 'test';
    $var2 = '1'; //might be 2, 3, 4 ...
    $var3 = '2'; //might be 1,2,3,4 ...

    //let's say we want to call $test1['2'] ... var1, var2 and var3 are beeing changed depande on $_GET ...

    $together = $var1.$var2[$var3];
    //together= test1[2] if we take a look or vars above...
    ?>


    But when I call together in for each (to call my array) I get error... I've tried to print $together... I can see only first part (var1) ... Then I've tried to print one by one

    print $var1;
    print $var2;
    print $var3;


    The result is test12... So variables are set properly...

    I guess there is a mistake when I make $together...

    Once again: var1, var2 & var3 are used to combain an ID to call an array. Var1 and Var2 are used for array's name, var3 is used to get part of array.
     
    EagerWolf, Jan 15, 2007 IP
  5. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #5
    $together isn't an array anymore, that's why foreach fails, it's a string ....
     
    krakjoe, Jan 15, 2007 IP
  6. EagerWolf

    EagerWolf Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    How should I solve it...

    The problem is ... when I print $together; I get value from first value ($var1) only...
     
    EagerWolf, Jan 15, 2007 IP
  7. EagerWolf

    EagerWolf Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    This might be a problem...

    $var2 is called from session...
     
    EagerWolf, Jan 15, 2007 IP
  8. jnestor

    jnestor Peon

    Messages:
    133
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #8
    If I understand your question correctly you need to use eval. Something like
    eval("\$together = \$$var1$var2[$var3]");

    so if: $var1="test'; $var2="1"; $var3="2" you'd get
    $together = $test1[2];
     
    jnestor, Jan 15, 2007 IP
  9. sukantab

    sukantab Well-Known Member

    Messages:
    2,075
    Likes Received:
    49
    Best Answers:
    0
    Trophy Points:
    110
    #9
    Try this :
    $var1 = 'test';
    $var2 = '1';
    $var3 = '2';

    $together=$var1.$var2."'".$var3."'";
     
    sukantab, Jan 16, 2007 IP
  10. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #10
    
    <?php
    /*
     IMO this is a much more efficient way to store your data
    */
    $test = array
    (
    "1" => array("smith0", "smith1", "smith2"),
    "2" => array("jones0", "jones1", "jones2")
    );
    /*
     Set from $_GET right ?
    */
    $var1 = 'test';
    $var2 = '1'; //might be 2, 3, 4 ...
    $var3 = '2'; //might be 1,2,3,4 ...
    /*
     Just so you can see how the array is structured now
    */ 
    echo "<pre>";
    print_r($test);
    echo "</pre>";
    /*
     calling eval on the vars will make $string available in the script, it does not echo content
    */
    eval ("\$string = $". $var1 ."[".$var2."][".$var3."];");
    // see ....
    echo $string;
    ?>
    
    PHP:
     
    krakjoe, Jan 16, 2007 IP
  11. EagerWolf

    EagerWolf Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Well thanks for your help...

    I had this problem ...

    I had to call

    $test1 = array (...);
    $test2 = array (...);
    etc.

    with three generated variables...

    var1 = test
    var2 = 1,2,3... etc
    var3 = 1,2,3... etc

    Than I've changed structure to:
    $test = array(
    '1' => array(...),
    '2' => array(...),...
    );

    So I used my variables: $var1[$var2][$var3] and it worked perfectly!

    Thanks for all your help!
     
    EagerWolf, Jan 16, 2007 IP