0

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);

}
Code Lover
  • 8,099
  • 20
  • 84
  • 154
  • All you need to do is to define the property before the loop. `$p_row->total = 0;`. You should really avoid using `@` since that can make debugging a massive pain (since it suppresses any potential error message that you do want while debugging. – M. Eriksson Dec 29 '20 at 06:27
  • @MagnusEriksson I see, but the `$p_row` is only available in the loop. How can I define it outside the loop? – Code Lover Dec 29 '20 at 06:30
  • 1
    Ah yes, you're correct. Well, if the properties doesn't exist before the loop, you can simply define it by using `=` instead of `+=` in the loop. If you use `+=`, it's adding to the existing value. But if that property doesn't exist (and that's always the case here), then just define the property instead of adding to it: `$p_row->total = gs_get_procuct_qty_price(....);` – M. Eriksson Dec 29 '20 at 06:47
  • @MagnusEriksson the `total` has to be calculated by the `qty` and the `price`. The `qty` is a property in `$p_row` and `price` I have to get it dynamically using the product id. So I can get the total for the array item (a record). Please have a look the modified question. Is that the correct way? – Code Lover Dec 29 '20 at 07:00
  • I wrote an answer instead. – M. Eriksson Dec 29 '20 at 07:10

3 Answers3

0

I think in foreach loop you can't add an index, you have to create index before loop. @ is just ignore Notices. And in last your code, a "=" is extra.

Ali Fakoor
  • 82
  • 2
0

since $this->data[$p_row->group_id]['total'] it is not defined first and you are $this->data[$p_row->group_id]['total']+= adding value to it it is throwing error. you can use this :

if(isset($this->data[$p_row->group_id]['total'])){
$this->data[$p_row->group_id]['total']+=gs_get_product_qty_price($p_row->product, $p_row->qty);
}else{
$this->data[$p_row->group_id]['total']= gs_get_product_qty_price($p_row->product, $p_row->qty);
}
Hammad Ahmed khan
  • 1,618
  • 7
  • 19
0

Since $p_row->total doesn't exist yet, define it instead of trying to adding to it. Simply change += to =.

foreach ($this->products as $p_row) {
    $this->data[ $p_row->group_id ][] = $p_row;
    $p_row->total = gs_get_product_qty_price($p_row->product, $p_row->qty);
}

When you use +=, PHP will first read the initial value of the property and adds to that value. But since the property doesn't exist, it tries to read from an undefined property, which will throw the warning you see. Changing to = defines and sets the value instead.

M. Eriksson
  • 13,450
  • 4
  • 29
  • 40