Is there anyway in PHP to create variable variables?

Discussion in 'PHP' started by Imozeb, Apr 9, 2010.

  1. #1
    Basicly I have data that I want to put into variables with names that increase like so.

    $id = 1;
    $name = 'bob';
    $pass = 'foo';

    $id = 2;
    $name = 'billy';
    $pass = 'bar';

    What I want is PHP to loop through the vars and output:

    $id_1 = 1;
    $id_2 = 2;
    $name_1 = 'bob';
    $name_2 = 'billy';
    $pass_1 = 'foo';
    $pass_2 = 'bar';

    How can I do this. Like is there anyway to append variables names depending on the loop number?

    Thanks.

    ~imozeb
     
    Imozeb, Apr 9, 2010 IP
  2. guardian999

    guardian999 Well-Known Member

    Messages:
    376
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #2
    you can use array
     
    guardian999, Apr 9, 2010 IP
  3. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #3
    Why dont you do it as an array of hashes;

    <?php
    $array=array();
    
    $array[0]['name']='bob';
    $array[0]['pass']='foo';
    $array[1]['name']='billy';
    $array[1]['pass']='bar';
    $count=0;
    
    foreach($array as $hash)
      {
      print "\$id_".$count." = ".$count."\n";
      print "\$name_".$count." = '".$hash['name']."'\n";
      print "\$pass_".$count." = '".$hash['pass']."'\n";
      $count++;
      }
    ?>
    PHP:
    OUTPUT:

    $id_0 = 0
    $name_0 = 'bob'
    $pass_0 = 'foo'
    $id_1 = 1
    $name_1 = 'billy'
    $pass_1 = 'bar'
    Code (markup):
     
    lukeg32, Apr 9, 2010 IP
  4. Cloud Computing Forum

    Cloud Computing Forum Guest

    Messages:
    55
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    PHP does support variable variables. Use $$ instead of $. Have a look on PHP.net
     
    Cloud Computing Forum, Apr 9, 2010 IP
  5. alberrambo

    alberrambo Peon

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    $$ is used to create varible of variable
     
    alberrambo, Apr 9, 2010 IP
  6. ThomasTwen

    ThomasTwen Peon

    Messages:
    113
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #6
    the $$ sign is terrible in terms of performance and code readability - use arrays in the way lukeg32 mentioned.
     
    ThomasTwen, Apr 9, 2010 IP
  7. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Okay. I've done as lukeg32 said and made an array.

    Now how do I store the PHP array in a javascript array so it can be used after the page has been loaded?
    Is it even possible?!?

    And how do you create a proper array?

    I been trying something like this:

    $array = array();

    while($n < 5)
    {
    ++$n;
    $array[$n]['1'] = $var;
    $array[$n]['2'] = $var2;
    //$var 1 and 2 are dynamic
    }
     
    Last edited: Apr 9, 2010
    Imozeb, Apr 9, 2010 IP
  8. David3

    David3 Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    @ThomasTwen
    Hello, I'm still learning PHP, so I don't understand what you mean regarding variable variables ("$$") and performance. Wouldn't the efficiency/performance be better compared to an array? Or I guess it depends on how many variable variables you have....
     
    David3, Apr 9, 2010 IP
  9. cheaplaptops

    cheaplaptops Member

    Messages:
    262
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    30
    #9
    use python
     
    cheaplaptops, Apr 9, 2010 IP
  10. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #10
    You need to serialize the data before passing it to javascript unless you are processing the javascript function within php.

    http://uk3.php.net/manual/en/function.serialize.php

    The above will give you;

    array[0][1] = $var
    $array[0][2] = $var2

    etc

    If you want to know which variable should belong in which the you are
    better off usinjg hash keys instead;

    $array[$n]['YOUR_KEY'] = $var;
    $array[$n]['YOUR_OTHER_KEY'] = $var2;
    PHP:
     
    lukeg32, Apr 10, 2010 IP
  11. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #11
    Using variable variables ($$) is not a sufficient solution to the ops problem - certainly nothing that will beat the array example given above.

    PHP's array processing is actually very very fast.
     
    lukeg32, Apr 10, 2010 IP
  12. ThomasTwen

    ThomasTwen Peon

    Messages:
    113
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #12
    The array system is highly performance optimized whilst the $$ feature is not.
     
    ThomasTwen, Apr 10, 2010 IP
  13. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Could someone give me an example of how to serialize data and put it in a javascript array?

    Thanks.
     
    Last edited: Apr 10, 2010
    Imozeb, Apr 10, 2010 IP
  14. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #14
    You can specify the elements and keys when you create the array if you prefer, its up to you.

    What is the problem you are getting? is it an error or is the array empty?

    Dont forget that, as in your example $n is your incremental counter; you must still do this inside the while (or for... ) loop.

    If you arent getting any errors, paste your code in, and ill take a look.
     
    lukeg32, Apr 10, 2010 IP
  15. Brandon.Add.On

    Brandon.Add.On Peon

    Messages:
    178
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Instead of using serialize as it's mostly for objects, try using json_encode if your PHP version supports it.
     
    Brandon.Add.On, Apr 10, 2010 IP
  16. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #16
    @lukeg32: I've fix the array, thanks for saying you'll help.

    Now all I have to do is figure out how to change it to a javascript array so I can use the values after the page has been loaded. How would I do this? Thanks.
     
    Imozeb, Apr 10, 2010 IP
  17. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #17
    Nonsense!

    Serialize is for any data structure!! (though, you are semi-correct, json_encode might also work)
     
    lukeg32, Apr 10, 2010 IP
  18. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #18
    No problem at all.

    What part are you having problems with? It would help better to understand what you are trying to achieve since there are various ways to do it; serialize is a standard way of passing a data structure from one place to another for example;

    Are you processing a form or such and then passing this data to an external site / entity or are you preloading the data through php to be used in a javascript function as the page loads? Do you have an example output/code of where you are having the problem?
     
    lukeg32, Apr 10, 2010 IP
  19. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #19
    Okay, here's what I am trying to do.

    I have created a function that creates an array from data from a database as:

    $array[$n]['name'] = $value;
    $array[$n]['pass'] = $value;

    This code is in a while loop which increments $n and changes $value. It works, because I checked the array using var_dump($array); and it returned the values I was expecting.

    Now what I am trying to do is take PHP $array and change it into a javascript array. I have to do this so that the array values are not lost after the page loads so that javascript can use the data to populate input fields depending on what the user selects in a dropdown input box.

    Basicly I need a way to transfer the PHP array into a Javascript array.

    Thanks!

    ~imozeb
     
    Imozeb, Apr 10, 2010 IP
  20. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #20
    Its the javascript part that doesnt make sense.....

    For example, there are multiple ways of processing the data. If you are passing from PHP to javascript and vice-versa then in many cases you should serialize the data..... If you have a dynamic page being created from PHP and you are preloading the javascript then you can either process it inside php with a custom routine or use json_encode.

    http://uk3.php.net/manual/en/function.serialize.php
    http://uk3.php.net/manual/en/function.json-encode.php

    There are of course plenty of other ways you could be doing this, hence the confusion, but you will almost certainly need one of the above.
     
    lukeg32, Apr 10, 2010 IP