0

I am just trying to create PHP variables dynamically. below is the code I have tried.

    if($BrickTerritorys)
    {
            foreach($BrickTerritorys as $index=>$BrickTerritory)
            {
                ${"$T.$index"}= $BrickTerritory->TerritoryID;
                ${"'Weightage'.$index"} = $BrickTerritory->Weightage;
            }
            echo $T1."-".$T2."--".$Weightage1."---".$Weightage2; exit;
    }

while 
$BrickTerritorys is 
 [1] => stdClass Object
        (
            [id] => 119
            [TerritoryID] => HYD-2-CMD
            [BrickCode] => 16
            [BrickName] => BUHURO
            [Weightage] => 40.00
            [BPCode] => bp00066
            [GroupCode] => CMD
        )
    [2] => stdClass Object
        (
            [id] => 36330
            [TerritoryID] => HYD-1-CMD
            [BrickCode] => 16
            [BrickName] => BUHURO
            [Weightage] => 60.00
            [BPCode] => bp00066
            [GroupCode] => CMD
        )

When I print in the last, nothing gets printed. Any help is much appreciated, please.

Thanks in advance

5
  • what does var_dump($GLOBALS); says? Commented Sep 18, 2018 at 6:24
  • 1
    Using variable variables is usually an indication of inappropriate array data storage. You are adding unnecessary convolution to your code. Commented Sep 18, 2018 at 6:33
  • ["'T'.1"]=> string(9) "HYD-2-CMD" ["'Weightage'.1"]=> string(5) "40.00" ["'T'.2"]=> string(9) "HYD-1-CMD" ["'Weightage'.2"]=> string(5) "60.00" result of Global variable... but still i didnt get my desired results..any help ?? Commented Sep 18, 2018 at 6:37
  • You are performing dynamic variable generation via a loop, but then hardcoding (expecting) just two values from two rows. Do you actually need variable variables? Commented Sep 18, 2018 at 6:44
  • @Salman do you realize that you have granted the green tick to an incorrect answer? Commented Sep 23, 2018 at 10:44

3 Answers 3

1

${"T$index"} as well as ${"Weightage$index"}

you don't need the dot,or you can use ${'T' . $index}. look at the dot. it's not addition operation while it in "". following this code:

if($BrickTerritorys)
{
    foreach($BrickTerritorys as $index=>$BrickTerritory)
    {
        ${"$T.$index"}= $BrickTerritory->TerritoryID;
        ${"'Weightage'.$index"} = $BrickTerritory->Weightage;
    }
    echo $T1."-".$T2."--".$Weightage1."---".$Weightage2; exit;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Your 1 point is wrong, it start from 1 and clearly mentioned in question detail. and your second point is absolutely OK, i it working now . Thumb up for you (y)
I don't like the quoting in this answer. I feel this answer is incorrect. Is this tested?
@Salman this answer is no good. It only produces Notices and not the correct output. Totally wrong. Proof: 3v4l.org/YEejB Please rethink your green tick for the good of this community.
Who keeps upvoting this incorrect answer? This will DEFINITELY mislead/confuse researchers.
1

Try changing those lines like this:

        ${"T$index"}= $BrickTerritory->TerritoryID;
        ${"Weightage$index"} = $BrickTerritory->Weightage;

In your code ${"$T.$index"} $T is searching for variable, and you should get undefined variable $T, so you have to remove $ sign, if you want to have T1, T2 variables.

After that, ${"'Weightage'.$index"}, the apostrophes between Weightage means your variable will look like 'Weightage'.1, 'Weightage'.2.. and etc.

1 Comment

@Salman This answer DOES provide the desired output WITHOUT Notices. Proof: 3v4l.org/i0rVo
1

This can be done a few different ways without variable variables AND produce a completely dynamic outcome.

Here's one: (Demo)

$array = (array)$BrickTerritorys;             // cast as array
$tids = array_column($array, 'TerritoryID');  // isolate column data
$was = array_column($array, 'Weightage');     // isolate column data
$merged = array_merge($tids, $was);           // add 2nd array data after 1st array data
foreach ($merged as $i => $v) {
    echo str_repeat('-', $i) , $v;            // increase hyphens on each iteration starting from 0
}

Output: (notice, no hardcoded echo)

HYD-2-CMD-HYD-1-CMD--40.00---60.00

Comments

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.