2

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.

7
  • Ack. Your variable naming confuses me greatly. I'm not quite sure what you're going for... can we try a more concise code sample that, rather than being your application code, contains just the principle you're going for? Commented May 18, 2010 at 14:42
  • not sure why the question was confusing or why it deserved a -1? I think there are a alot of people who previously did not understand arrays would benefit Commented May 18, 2010 at 15:06
  • @JM4: You already have the solution to your problem in the code. After all $_SESSION is just that: a variable that includes other variables. Commented May 18, 2010 at 15:30
  • @Konrad Rudolph - it does not for the following reason: I store everything in SESSION without any variable names so in order to call the 16th person's city, I would have to write $_SESSION['F16City'] and write out tons of code which is exactly what I was trying to avoid. Commented May 18, 2010 at 15:37
  • @JM4: That’s not what I mean. You don’t need to use $_SESSION (in fact you should not) but $_SESSION is essentially the same as your solution (i.e. an associative array). The same, in fact, as $names in the solution that you accepted below. Commented May 18, 2010 at 15:40

6 Answers 6

3

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

Sign up to request clarification or add additional context in comments.

4 Comments

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.
For example, you would print the firstname of the 2nd person with: echo $names[2]['fname'];
Or to iterate through all names: foreach($names as $entry) { echo $entry['fname']; }
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?
3

what about arrays? more: arrays of objects?

1 Comment

+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?
1

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

${'f'.$j.'fname'} = $_SESSION["F$jFirstName"];

4 Comments

...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 - 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.
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).
i agree, this method is ugly, arrays are a much better idea.
1

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"];

2 Comments

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.
$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.
0

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 = ...

5 Comments

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.
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.
Iterating through an array is much easier than iterating through numbered variables. Have you worked with arrays before? php.net/array
@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]
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...
0

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.
}
?>

2 Comments

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
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.