In foreach loop, I am trying to add some additional property for the source array or objects. That gives me the following notice.
Notice: Undefined property: stdClass::$total
foreach ($this->products as $p_row) {
$this->data[ $p_row->group_id ][] = $p_row;
// getting index error here
$p_row->total += gs_get_product_qty_price($p_row->product, $p_row->qty);
}
However, if I add annotation @
the error is gone.
foreach ($this->products as $p_row) {
$this->data[ $p_row->group_id ][] = $p_row;
// adding @ the error gone
@$p_row->total += gs_get_product_qty_price($p_row->product, $p_row->qty);
}
As far as I understood is on the first iteration, it is not defined; maybe that is why showing an error.
Can anyone explain to me to clear my thought, and is it okay to use
@
to avoid error?
The same notice occurs if I try to set data in
Notice: Undefined index: total
$this->data[$p_row->group_id]['total'] += gs_get_product_qty_price($p_row->product, $p_row->qty);
Is it the solution?
foreach ($this->products as $p_row) {
$p_row->total = 0;
$this->data[ $p_row->group_id ][] = $p_row;
$p_row->total += gs_get_product_qty_price($p_row->product, $p_row->qty);
}