I want to create new object keys in foreach loop so I write:
$all_variants = $p->variants;
foreach ($all_variants as $a) {
$a->discount_price = $a->price;
$a->original_price = $a->price;
$a->button_text = 'BUY';
}
but this code wont create new keys (discount_price, original_price, button_text) … $all_variants->discount_price
is undefinded ….
Whats bad in my code?
3
Answers
This works for me:
You can use next trick for this
Here object converted to array modified and converted to object again
php code online test
You can modify the original array, but not the copy of the element you get in the loop…