Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    May 2005
    Posts
    224
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Can you include variables in variable titles? [SOLVED]

    I have a foreach loop, and it needs to output different variables for each array value.

    i.e.

    array has the keys one, two, three

    I need the foreach to output variables like:

    $onefinal $twofinal $threefinal.

    How would I include the key in the variable name?



    EDIT: Solved my problem!
    I changed the variable I needed to be unique to
    ${$value}
    that way, each of the values of the array would be its own unique variable

    here's a snippit:
    PHP Code:
    $sums[one] = "Q1";
    $sums[two] = "Q2";
    //etc..
    foreach($sums as $key => $value){
    $sumq "SELECT SUM($value) FROM results";
    $sumr mysql_query($sumq);
    $arr mysql_fetch_row($sumr);
    $finalvalue $arr[0];
    $avg $finalvalue $num;
    ${
    $value} = round($avg2);

    that gave me unique variables named $Q1, $Q2, etc.
    Last edited by Crazydog; 06-30-2007 at 01:40 AM.

  • #2
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    PHP Code:
    $num count($array);
    for(
    $i 0;$i $num;$i++)
    {
    $string key($item);
    $
    $string $item;
    next($array);

    Hopefully that works for you tho I didn't test it...

    It works based on the principle of Variable variables.

    HTH
    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION


  •  

    Posting Permissions

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