Reputation: 1276
please help me figure this out. I just want to get the total quantity of the same inventory_record_id, as the image is shown below. And I wanted to store that total to an array name $salesQtyArrayHldr
. However, it always return the undefined offset 0 error. Please help. Here is my code.
<?php
$salesQtyArrayHldr = array();
?>
@foreach($inventory as $val)
<?php
for($i = 0; $i < count($val->sales); $i++){
if($val->id == $val->sales[$i]->inventory_record_id ){
$salesQtyArrayHldr[$i] += $val->sales[$i]->quantity;
}
}
var_dump($salesQtyArrayHldr);
?>
Error
Upvotes: 0
Views: 252
Reputation: 1276
for($i = 0; $i < count($val->sales); $i++){
if($val->id == $val->sales[$i]->inventory_record_id ){
if(isset($salesQtyArrayHldr[$val->id])){
$salesQtyArrayHldr[$val->id] += $val->sales[$i]->quantity;
}else{
$salesQtyArrayHldr[$val->id] = 0;
$salesQtyArrayHldr[$val->id] += $val->sales[$i]->quantity;
}
}
}
Upvotes: 0
Reputation: 598
Probably there is no such key in array $val->sales Use helper dd() to check the structure of this variable.
Upvotes: 0