tags:

views:

93

answers:

6

I am trying to create a loop statement for some of my code and am wondering how I can put a variable within another variable.

For example:

<?php 
$j=1;
while ($j <= 9): {

$f$jfname = $_SESSION['F$jFirstName'];
$f$jmi = $_SESSION['F$jMI'];
$f$jlname = $_SESSION['F$jLastName'];
}
 $j++; endwhile; 
?>

Where the goal is to have the j variable increase during the loop and change the values as:

$f1fname $f2fname $f3fname

and so on.

Any ideas?

EDIT

Yes I am aware that my INITIAL form was flawed in the way I captured information (as individual variables as opposed to arrays) so any answer telling me that SESSION is an array and so on is irrelevant because I cannot call any variables implicitly from the SESSION variables I created (without a line by line reference) All variable stored in SESSION are completely unique and independent of each other.

+3  A: 

what about arrays? more: arrays of objects?

Andrey
+1: Arrays are the correct way to solve this problem. More specifically, `$_SESSION` can store arrays just as easily as single values, so why not just set/retrieve an array from it instead?
R. Bemrose
A: 

i don't know why you need this, but i you thought about it and realy want to do that, you just have to write

$varname = "f".$j."fname"; 
$$varname = ...

instead of

$f$jfname = ...
oezi
it is ugly, sorry.
Andrey
i know its ugly, i just answered his question...
oezi
This seems to be what the OP wants... but I would warn you that this is generally considered to be a bad idea and can lead to painful and difficult-to-diagnose bugs.
JSBangs
reason i need numbered variables is because i have 30 first names (among several other variables) I want to run through. I currently have all the code listed out but the file sizes are getting huge.
JM4
Iterating through an array is much easier than iterating through numbered variables. Have you worked with arrays before? http://php.net/array
Matchu
@Matchu - I only have started to. Just started trying to write code a month ago but I am using fpdf to output some of the SESSION variables. Remembering that $f1fname is the first person's first name and where it needs to go in the PDF is much easier for me to remember than array[12]
JM4
i know what arrays are, i know this is a dumb idea and i would never use this, because i know its very, very dumb - but it't the answer to the question above, so i don't understand why i get downvotes for this...
oezi
A: 

Oh, wait, I think I get it now.

How about we just have an array to contain it? Variable variables are messy enough, don't you think?

<?php
$f = array();
for($j = 1; $j <= 9; $j++) {
  $f[$j] = array();
  $f[$j]['fname'] = $_SESSION['F' . $j. 'FirstName'];
  // etc.
}
?>
Matchu
@Matchu - I think your reasoning makes sense, i was just trying to consolidate lines of code though. I have 12 variables I want to run through the 'while' loop up top and creating the arrays you mention seems like it will take up just as many lines of code if I want to run through all the variables. Perhaps not but I can give it a shot
JM4
It's definitely easier to iterate through an array when you're done than to iterate through numbered variables - or worse, type out all of those variable names yourself :o
Matchu
+1  A: 

php wont parse variables inside single quotes ', use double quotes " and try this format...

${'f'.$j.'fname'} = $_SESSION["F$jFirstName"];
Galen
...this is actually supported? Ew, ew, ew. +1, since it answers the original question well, but please, please, please, no one ever actually do this.
Matchu
@Matchu - why do you dislike this answer? It actually accomplishes every goal I am trying to go for but am curious why it is bad practice.
JM4
Arrays are a standard way of accomplishing what you want, and you see it everywhere in the wild. This format *never, ever* appears in the wild, and for good reason: *this is what arrays are for*. As such, arrays come with all the extra goodies like free iteration and `array_search` and the like. No need to duplicate the functionality of arrays while robbing yourself of extra behavior and confusing any future developer (trust me, I'd be stumped for a good 15 minutes).
Matchu
i agree, this method is ugly, arrays are a much better idea.
Galen
+2  A: 

Didn't test the code myself but perhaps something like:

<?php 
$names = array();
for ($j=1; $j <= 9; $j++) {
   if (! isset($names[$j]))
     $names[$j] = array();
   $names[$j]['fname'] = $_SESSION["F{$j}FirstName"];
   $names[$j]['mi'] = $_SESSION["F{$j}MI"];
   $names[$j]['lname'] = $_SESSION["F{$j}LastName"];
}
?>

Then you have an array with arrays of the userinfo

baloo
I think this would work well but I am just so unfamiliar with how to then call on each individual variable (I am currently printing $f1fname on a PDF file by specific location and so on with over 300 variables. Not quite sure how to do this using arrays.
JM4
For example, you would print the firstname of the 2nd person with:echo $names[2]['fname'];
baloo
Or to iterate through all names:foreach($names as $entry) { echo $entry['fname']; }
baloo
@baloo - I think this would normally work but want to run by how I'm using this. I place each variable (1st person first name) at X and Y coordinates on the page. I am unable to 'loop' the write statements because they X and Y coordinates different for every single variable so while I can change the name of the variable being input to $names[1]['fname'], I am not sure of the benefit when compared to $f1fname. Thoughts?
JM4
+1  A: 

To references a variable given its name, you can do it as follows:

  • Directly:

    $result=$myVar;
    
  • From String:

    $result="$myVar";
    
  • From Variable:

    $a='myVar';
    $result=$$a;
    

And your code....

    // preferred way
    $results=array();
    for ($j=1; $j<=9; $j++) {
        $result=array();
        $result['jfname'] = $_SESSION['f'.$j.'FirstName'];
        $result['jmi'] = $_SESSION['f'.$j.'MI'];
        $result['lname'] = $_SESSION['f'.$j.'LastName'];
        $results[$j]=$result;
    }

By the way,

$_SESSION['f'.$j.'LastName'];

Is the same as

$_SESSION["f{$j}LastName"];
Christian Sciberras
@Christian - thank you for your code. The overall idea is helpful but the code is a bit flawed. For example, In the result array, ['jfname'] is improper context and would result in jfname, instead of 1fname or 2fname as needed.
JM4
$results[$j]['fname'] is more "standard", since having different variable indexes isn't preferable.It might depend on implementation, but I still believe it is the best approach rather than using indexes as the name of a variable.
Christian Sciberras