0

I have these loop statement to generate arrays labeled with z variable. array will be sent to ajax to be displayed in HTML page.

    for($z=0;$z<5;$z++){    
        $billsum = $bills['amount'];
         $bill['$z'] = [$billsum];      
    }

How can I generate arrays such as $bill0,$bill1,$bill2,$bill3,...etc

7
  • remove the ' around $z: $bill[$z] = $billsum; (and the [] - or do you really want another nested array in there?) Commented Oct 25, 2018 at 15:34
  • 1
    you are aware, that $billsum will allways be the same?? Commented Oct 25, 2018 at 15:38
  • since you accepted an answer below (which is correct) I just wanted to tell you, that it's a very bad idea to have variables named $bill0, $bill1, ... This is what arrays are for! Commented Oct 25, 2018 at 16:03
  • @Jeff would you please explain. Thx Commented Oct 25, 2018 at 16:04
  • it's far better and more practical to have an array $bills that contains each bill in $bill[0], $bill[1],... (or maybe you want an array with only billsums, then call it $billsums[1], .. Commented Oct 25, 2018 at 16:06

2 Answers 2

2

$z shouldn't be inside quotes.

for($z=0;$z<5;$z++){    
        $billsum = $bills['amount'];
        $bill[$z] = [$billsum];      
}
Sign up to request clarification or add additional context in comments.

3 Comments

PHP Notice: Undefined variable: bill0
What line is that?
I'm sending the array back to ajax. the correct way is marked already thx
1
for($z=0;$z<5;$z++){    
        $billsum = $bills['amount'];
        ${'bill'.$z} = [$billsum];      
}

So, now you will have arrays like $bill0,$bill1,$bill2,$bill3...

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.