Php 如何获得相同的数组?拉威尔
我有一辆手推车。add函数将创建以下数组:Php 如何获得相同的数组?拉威尔,php,arrays,laravel,Php,Arrays,Laravel,我有一辆手推车。add函数将创建以下数组: __PHP_Incomplete_Class Object ( [__PHP_Incomplete_Class_Name] => App\Classes\Cart [items] => Array ( [3] => Array ( [name] => LG F2T3HS6W
__PHP_Incomplete_Class Object
(
[__PHP_Incomplete_Class_Name] => App\Classes\Cart
[items] => Array
(
[3] => Array
(
[name] => LG F2T3HS6W
[qty] => 1
[prod_url] => lg_f2t3hs6w
[code_cat] => large-home-appliances
[url_cat] => washing-machines
[img] => img_10.jpg
[cost] => 380
)
)
[totalQty] => 380
[totalPrice] => 380
)
代码如下:
public function add($item, $id){
$storedItem = [
'qty' => 0,
'id' => $item->id,
'prod_url' => $item->url,
'code_cat' => $item->category->code,
'url_cat' => $item->category->url,
'name' => $item->name,
'cost' => $item->price,
'price' => $item->price,
'img' => $item->cardImage->path
];
if($this->items){
if(array_key_exists($id, $this->items)){
$storedItem = $this->items[$id];
}
}
$storedItem['qty']++;
$storedItem['cost'] = $item->price * $storedItem['qty'];
$this->items[$id] = $storedItem;
$this->totalQty++;
$this->totalPrice += $item->price;
}
我对数组之外的内容感兴趣:totalPrice
和totalQty
我有一个模态窗口。在ajax的帮助下,它还生成一个订单。在这里,我试图制作相同的数组,以避免在管理面板中显示订单时出错
阿贾克斯:
CartController:
public function modal_order_place(Request $request){
$product = Product::find($request->id);
$selprod['items'] = array(
$request->id => array(
'name' => $product->name,
'qty' => $request->qty,
'prod_url' => $product->url,
'code_cat' => $product->category->code,
'url_cat' => $product->category->url,
'img' => $product->cardImage->path,
'cost' => $product->price*$request->qty
)
);
$object = (object)$selprod;
$order = new Order();
$order->cart = serialize($object);
$order->name = $request->username;
$order->email = $request->email;
$order->phone = $request->phone;
Auth::check()?Auth::user()->orders()->save($order):$order->save();
return response()->json([
'notif_text' => 'Your order is being processed'
]);
}
在控制器中,我尝试制作这个数组。所以我会把它做成和购物车一样的形状。虽然看起来是这样的:
stdClass Object
(
[items] => Array
(
[3] => Array
(
[name] => LG F2T3HS6W
[qty] => 1
[prod_url] => lg_f2t3hs6w
[code_cat] => large-home-appliances
[url_cat] => washing-machines
[img] => img_10.jpg
[cost] => 380
)
)
)
stdClass Object
(
[items] => Array
(
[3] => Array
(
[name] => Стиральная машина LG F2T3HS6W
[qty] => 1
[prod_url] => lg_f2t3hs6w
[code_cat] => large-home-appliances
[url_cat] => washing-machines
[img] => img_10.jpg
[cost] => 380
)
[totalQty] => Something
[totalPrice] => Something
)
)
当我尝试这样做时:
$request->id => array(
'name' => $product->name,
'qty' => $request->qty,
'prod_url' => $product->url,
'code_cat' => $product->category->code,
'url_cat' => $product->category->url,
'img' => $product->cardImage->path,
'cost' => $product->price*$request->qty
),
'totalQty' => Something,
'totalPrice' => Something
);
结果是这样的:
stdClass Object
(
[items] => Array
(
[3] => Array
(
[name] => LG F2T3HS6W
[qty] => 1
[prod_url] => lg_f2t3hs6w
[code_cat] => large-home-appliances
[url_cat] => washing-machines
[img] => img_10.jpg
[cost] => 380
)
)
)
stdClass Object
(
[items] => Array
(
[3] => Array
(
[name] => Стиральная машина LG F2T3HS6W
[qty] => 1
[prod_url] => lg_f2t3hs6w
[code_cat] => large-home-appliances
[url_cat] => washing-machines
[img] => img_10.jpg
[cost] => 380
)
[totalQty] => Something
[totalPrice] => Something
)
)
如何将此数据从数组中取出,但保留在对象中?您正在将
totalQty
和totalPrice
添加到与您的“$request->id”相同的“级别”。
这样做应该会得到预期的结果:
$selprod['totalQty']=Something;
$selprod['totalPrice']=某物;
成功了!非常感谢。