简体   繁体   English

array_key_exists() 期望参数 2 是数组,null 给定 - Cakephp 3

[英]array_key_exists() expects parameter 2 to be array, null given - Cakephp 3

Friends, I appreciate any comments.朋友们,我感谢任何意见。

I'm having problems with array_key_exists.我遇到了 array_key_exists 的问题。 I need to check if a product already exists in the shopping cart.我需要检查购物车中是否已经存在产品。 I'm doing it this way:我这样做是这样的:

.
$session = $this->request->getSession();
            $cart = $session->read( 'cart' );
            
            if (array_key_exists($order->product_id, (array) $cart)) {
                $this->Flash->error('Invalid request');

            } else {
                $cart[] = $order;
                $session->write('cart', $cart );
                $this->Flash->success($order->product->name_product . ' has been added to the shopping cart');
                return $this->redirect( ['action' => 'index'] );

            }
            return $this->redirect($this->referer());
        } else {
            return $this->redirect(['action' => 'index']);
        }
.
.

I received the error below, but now with the update it no longer exists.我收到了下面的错误,但现在随着更新它不再存在。 But the function still doesn't help me.但是 function 仍然对我没有帮助。

array_key_exists() expects parameter 2 to be array, int given

The product is added to the cart even if it has already been added.即使产品已添加,该产品也会添加到购物车中。 I need to compare the selected product id with the product id that is already in the cart.我需要将所选产品 ID 与购物车中已有的产品 ID 进行比较。 The order information is basically: id, quantity, user_id, product_id.订单信息基本是:id、数量、user_id、product_id。

I am grateful if anyone can analyze some way to check the id in the array that is mounted.如果有人可以分析某种方法来检查已安装数组中的 id,我将不胜感激。

When you do this:当你这样做时:

$cart[] = $order;

you are asking PHP to add a new element to the end of your $cart array, giving it the next available numeric key.您要求 PHP 在$cart数组的末尾添加一个新元素,为其提供下一个可用的数字键。 You are then trying to see if the product id exists as a key in the array.然后,您尝试查看产品 ID 是否作为数组中的键存在。 Unless your product ids are like 0 and 1, that's unlikely to ever happen.除非您的产品 ID 像 0 和 1,否则这不太可能发生。 If you want your cart to be keyed by product id, use如果您希望您的购物车按产品 ID 键入,请使用

$cart[$order->product_id] = $order;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 array_key_exists()期望参数2为数组,给定null - array_key_exists() expects parameter 2 to be array, null given array_key_exists() 期望参数 2 是给定的数组字符串 - array_key_exists() expects parameter 2 to be array string given Array_key_exists()期望参数2为数组,给定为null - Array_key_exists() expects paramete 2 to be array, null given 警告:array_key_exists() 期望参数 2 为数组,空 - Warning: array_key_exists() expects parameter 2 to be array, null 致命错误 [0]:array_key_exists() 期望参数 2 是数组,给定 null - Fatal error [0]: array_key_exists() expects parameter 2 to be array, null given CodeIgniter 3登录类引发array_key_exists()期望参数2为数组,给定null - CodeIgniter 3 Login Class Throws array_key_exists() expects parameter 2 to be array, null given array_key_exists() 期望参数 2 是数组 - array_key_exists() expects parameter 2 to be array 如何在PHP中调试“ array_key_exists()期望参数2为数组,给定整数”? - How to debug “array_key_exists() expects parameter 2 to be array, integer given” in PHP? 警告:array_key_exists()期望参数2为数组,给定布尔值。 WordPress的 - Warning: array_key_exists() expects parameter 2 to be array, boolean given. Wordpress array_key_exists()期望参数2为数组,使用pushWoosh类时为字符串 - array_key_exists() expects parameter 2 to be array, string when using pushWoosh Class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM