Issue With Array in laravel

Viewed 37

I am using an array for display in my project, but it is not working properly because of a 1 at the end it is not there in the controller output but is in the blade output. Please can someone answer my query why 1 is there in the last part of the array.

Here is the controller/PHP array

$session = new Session();
$cam = Compare::where('session_id',$session->get('shop_session'))->get('product_id');
$pd_id = [];
$pdt1 = [];
$pdt = [];
$pd_desc = [];

foreach($cam as $ca)
{
   $pd_id[] = $ca->product_id;
}

foreach($pd_id as $pid)
{
   $pdt_query = Product::where('id',$pid);
   $pdt1['image'] = $pdt_query->pluck('product_thumbnail')[0];
   $pdt1['name'] = $pdt_query->pluck('product_name_en')[0];
   $pdt1['currency'] = $pdt_query->pluck('currency')[0];
   $pdt1['selling_price'] = $pdt_query->pluck('selling_price')[0];
   $pdt1['discount_price'] = $pdt_query->pluck('general_discount_price')[0];
   $pdt1['id'] = $pid;
   $pdt[] = $pdt1;

   $pd_desc['Description'][] = Product::where('id',$pid)->pluck('long_descp_en')[0];
   $pd_desc['Tags'][] = Product::where('id',$pid)->pluck('product_tags_en')[0];
   $pd_desc['Ratings & Reviews'][] = NULL;
   $pd_desc1['size'] = Product::where('id',$pid)->pluck('product_size')[0];
   $pd_desc1['color'] = Product::where('id',$pid)->pluck('product_color')[0];
   $pd_desc['Variants'][] = $pd_desc1;
   $pd_desc['Availability'][] = ((Product::where('id',$pid)->pluck('product_qty')[0])>1) ? 'In Stock' : 'Out of Stock';
   $pd_desc['Remove'][] = $pid;
}

$pty['name'] = NULL;
array_push($pdt,$pty);
$pd_desc = json_encode($pd_desc);
return view('body.compare',compact('pdt','pd_desc'));

Controller Output

{"Description":["klo","demo"],"Tags":["klo","demo"],"Ratings & Reviews": [null,null],"Variants":[{"size":null,"color":null} 
{"size":null,"color":null}],"Availability":["In Stock","In Stock"],"Remove":[22,21]}

Here is the output the blade file

First Array

Array
(
 [0] => Array
     (
        [image] => `enter code here`img/backend/upload/products/main_img/22_1635763190.jpg
        [name] => klo
        [currency] => ₹
        [selling_price] => 875
        [discount_price] => 0
        [id] => 22
    )

 [1] => Array
    (
        [image] => img/backend/upload/products/main_img/21_1635763121.jpg
        [name] => demo
        [currency] => ₹
        [selling_price] => 875
        [discount_price] => 456
        [id] => 21
    )

[2] => Array
    (
        [name] => 
    )

)
1

Second Array Output

{"Description":["klo","demo"],"Tags":["klo","demo"],"Ratings & Reviews":[null,null],"Availability":["In Stock","In Stock"],"Remove":[22,21]}1
1 Answers

Because this line :

$pty['name'] = NULL;

falls outside your loop. In your example your original query returns two results :

$cam = Compare::where('session_id',$session->get('shop_session'))->get('product_id');

And the loop therefore runs twice. After the loop finishes, however, you have this :

$pty['name'] = NULL;

which adds one final element to the array.

Related