Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vilom10252's avatar

How to check if an array contains empty values

I'm using Laravel 5.8 to build my Online Store and in this Online Store there are two types of products. Physical products such as (books, magazines & etc) and Video type products.

Now I need to determine that if a user has added a Video type product and ALSO a Physical product to his Shopping Cart, a session called physical-video submitted.

And if the Cart ONLY contains Physical products, submit the session physical with value of 1 otherwise submit the value of 0 for it.

$product_videos = [];

$foundOrder = Cart::select('crt_content')->whereCrtId($cart->crt_id)->latest()->first();
$foundOrder = json_decode($foundOrder->crt_content);

foreach ($foundOrder as $key => $value) {
   $product = Product::with('uploaded')->wherePrdId($value->id)->select('*')->first();

   array_push($product_videos, $product->prd_video);

   $prds[] = [
      $product,
      $value->quantity,
      $value->price
   ];

   $product_videos = array_unique($product_videos);

   $pvWithoutNull=array_filter($product_videos,function($_v){return $_v !== null;}); //$product_videos without any null value
   if(in_array(null, $product_videos)){
		if(empty($pvWithoutNull))
			Session::put('physical', 1); // only physical
		else
			Session::put('physical-video', 1); // physical-video
	}else{
			Session::put('physical', 0); // only video
    }
}

But it does not work properly because if I add ONLY Physical products to the Cart, the session Session::put('physical', 1); does not submitted and otherwise Session::put('physical-video', 1); submitted wrongly.

And if I dd($pvWithoutNull), I get this result for only Physical products:

capture

So how can I check this submit physical session with value of 1 properly if only Physical products added to Cart?

0 likes
1 reply
newbie360's avatar

@vilom10252

i haven't read all the logic, but store the session as array can solve your problem?

session(['colors' => ['red' => 1, 'blue' => 0, 'green' => 1]]);

dd(session('colors') , array_filter(session('colors')));

// return
array:3 [▼
  "red" => 1
  "blue" => 0
  "green" => 1
]

array:2 [▼
  "red" => 1
  "green" => 1
]

Please or to participate in this conversation.