【问题标题】:Array key error in Laravel 8 array_key_exists()Laravel 8 array_key_exists() 中的数组键错误
【发布时间】:2021-09-22 03:21:56
【问题描述】:

我得到 array_key_exists() 期望参数 2 是数组,给定 null 在下面的代码中。我知道这是因为$this->items;为空,但我不确定如何修复它。

警告:array_key_exists() 期望参数 2 为数组,给定为空

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

【问题讨论】:

    标签: arrays laravel


    【解决方案1】:

    查看完整的课程以及您对add() 函数的使用会很有帮助。

    如果$this->itemsnull,我希望您传递给构造函数的$cart 对象具有null 项。

    假设这是你的整个班级;

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

    如果您考虑下面的 sn-p,此代码将给出您遇到的相同错误。

    $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
    

    如果$cart->items属性中有项目,则不会抛出错误;

    $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
    

    通过在构造函数中使用dd()print_r() 来检查构造函数输入以检查$cart 参数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-20
      • 2016-10-07
      • 2021-08-18
      • 2020-12-31
      • 1970-01-01
      • 2019-02-04
      • 2021-08-29
      相关资源
      最近更新 更多