1

Want to create a php variable using text before and after the value of another variable.

variable variables. But have only seen examples of assignment with no text.

$vsBOA_W[]=$rows['vsBOA_W'];

// BOA = team 3-char abbreviation. Looking for something similar to above but insert 3-char abbreviations based on a input file.

$numOfTeams = 3;    // Determined from external source

$teamAbbr = array("BOA","CAA","CHN");   // For simplicity for this example. This array would normally be created from an external source. 

for($i=0; $i<$numOfTeams; $i++) {   // I know I can use size of array instead of nunOfTeams. That's not the issue. 

    echo $teamAbbr[$i]."<br>";      // for testing

    $$("vs".{'$teamAbbr[$i]'}."_W[]"} = $rows['$$("vs".{'$teamAbbr[$i]'}."_W"}']; // a total guess
}

I expect the end result to look like:

$vsBOA_W[]=$rows['vsBOA_W'];

for BOA

Update #2: I tried the following (breaking down each step) and get the same error on $$TeamWins assignment.

for($i=0; $i<$numOfTeams; $i++) {    
    echo $teamAbbr[$i]."<br>";      
    $TeamWins = 'vs' . $teamAbbr[$i] . '_W';
    echo "TeamWins=$TeamWins<br>";
    $TeamWinsHold = $rows[$TeamWins];
    echo "TeamWinsHold=$TeamWinsHold<br>";
    $$TeamWins[] = $TeamWinsHold;
}

Update #3:

for($i=0; $i<$numOfTeams; $i++) {    
    echo $teamAbbr[$i]."<br>";      
    $TeamWins = 'vs' . $teamAbbr[$i] . '_W';
    echo "TeamWins=$TeamWins<br>";
    $TeamWinsHold = $rows[$TeamWins];
    echo "TeamWinsHold=$TeamWinsHold<br>";
    ${$TeamWins}[] = $TeamWinsHold;
}

foreach(${$TeamWins} as $value) {
    echo "value=$value<br>";   // only displays last element or value assigned from above loop.
}

Update #4 (final):

$teamW = array();
$teamL = array();
for($i=0; $i<$numOfTeams; $i++) {    
    //echo $teamAbbr[$i]."<br>";        
    $teamWName = 'vs' . $teamAbbr[$i] . '_W';
    $teamLName = 'vs' . $teamAbbr[$i] . '_L';
    //echo "teamWName=$teamWName<br>";
    //echo "teamLName=$teamLName<br>";
    $teamW[$teamWName] = $rows[$teamWName];
    $teamL[$teamLName] = $rows[$teamLName];
}
2
  • For clarification, I wish to make this code generic for multiple tables. One table has a set of teams (ex: BOA,CAA,CHN), another table has another set of teams (ex: ABC,DEF,GHI). The external file tells the php code which teams to process (could be all or just a few). Commented Jul 27, 2019 at 4:14
  • Try ${$TeamWins}[]. Commented Jul 27, 2019 at 18:07

1 Answer 1

1

I don't quite understand the interplay with the rows in your example. But going by your guess assignment, you can always simplify, by forming the variable name upfront:

<?php

$rows  = ['xFOOy'=>[], 'xBARy'=>[], 'xBAZy'=>[]];
$items = ['FOO', 'BAR', 'BAZ'];

foreach($items as $abbr)
{
    $name = 'x' . $abbr . 'y';
    ${$name}[] = $rows[$name];
}

But, I'd say you'd be better off with a keyed array than variable variables, as it makes for easier inspection, and there is less chance of namespace clashes.

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

10 Comments

The reasons for the rows reference, $rows['vsBOA_W'], is that I'm looping through all the years / records for a team and displaying in a form as an array, $vsBOA_W[], which can be updated.
The $$name[] = $rows[$name]; returns an error:Cannot use [] for reading
See my Update #2.
@user2140857, you must be using an end of life PHP version, edited to address that error.
Correct. Using PHP 5.6. It's not production code. Never will be. Never had a requirement to go 7.0. Maybe I do now?
|

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.