Array key error in Laravel 8 array_key_exists()

Viewed 1405

I am getting array_key_exists() expects parameter 2 to be array, null given in the following code. I know it's because $this->items; is null but I am not sure how to fix it.

Warning: array_key_exists() expects parameter 2 to be array, null given

public function __construct($cart = null)
{
    if ($cart) {
        $this->items = $cart->items;
        $this->totalPrice = $cart->totalPrice;
        $this->totalQty = $cart->totalQty;
    } else {
        $this->items = [];
        $this->totalQty = 0;
        $this->totalPrice = 0;
    }
}

public function add($product)
{
    $item = [
        'id' => $product->id,
        'name' => $product->name,
        'price' => $product->price,
        'qty' => 0,
        'image' => $product->image

    ];
    if (!array_key_exists($product->id, $this->items)) {
        $this->items[$product->id] = $item;
        $this->totalQty += 1;
        $this->totalPrice += $product->price;
    } else {
        $this->totalQty += 1;
        $this->totalPrice += $product->price;
    }
    $this->items[$product->id]['qty'] += 1;
}
1 Answers

It would be helpful to see the full class and your usage of the add() function.

If $this->items is null, I would expect that the $cart object you are passing into the constructor has null items.

Let's assume this is your whole class;

class Cart
{
  public function __construct($cart = null)
  {
    if ($cart) {
      $this->items = $cart->items;
      $this->totalPrice = $cart->totalPrice;
      $this->totalQty = $cart->totalQty;
    } else {
      $this->items = [];
      $this->totalQty = 0;
      $this->totalPrice = 0;
    }
  }

  public function add($product)
  {
    $item = [
      'id' => $product->id,
      'name' => $product->name,
      'price' => $product->price,
      'qty' => 0,
      'image' => $product->image,
    ];
    if (!array_key_exists($product->id, $this->items)) {
      $this->items[$product->id] = $item;
      $this->totalQty += 1;
      $this->totalPrice += $product->price;
    } else {
      $this->totalQty += 1;
      $this->totalPrice += $product->price;
    }
    $this->items[$product->id]['qty'] += 1;
  }
}

If you consider the snippet below, this code will give the same error you are experiencing.

$product = (object) [
  'id' => 1,
  'name' => 'product',
  'price' => 100,
  'qty' => 0,
  'image' => 'image.jpg',
];

$existingCart = (object) [
  'items' => null, // this is most likely your issue
  'totalPrice' => 1000,
  'totalQty' => 1,
];

$cart = new Cart($existingCart);

$cart->add($product); // TypeError: array_key_exists(): Argument #2 ($array) must be of type array, null given

If you have items in the $cart->items property, the error won't be thrown;

$product = (object) [
  'id' => 1,
  'name' => 'product',
  'price' => 100,
  'qty' => 0,
  'image' => 'image.jpg',
];

$existingCart = (object) [
  'items' => [
    'product-1' => $product, // we actually have an array here of products. An empty array would also be fine
  ],
  'totalPrice' => 1000,
  'totalQty' => 1,
];

$cart = new Cart($existingCart);

$cart->add($product); // null

Double check your constructor input by using dd() or print_r() in your constructor to check the $cart argument.

Related