【问题标题】:How do I get the same array? php, Laravel如何获得相同的数组? php, Laravel
【发布时间】:2021-01-17 17:44:21
【问题描述】:

我有一辆购物车。 add 函数将创建以下数组:

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

我对数组之外的内容感兴趣:totalPricetotalQty

我有一个模态窗口。在ajax的帮助下,它还生成了一个订单。我正在尝试制作相同的数组,以避免在管理面板中显示订单时出错。

阿贾克斯:

$('.modal.fade').find('.submitModal').click(function (event) {
        event.preventDefault();
        let id=parseInt($('.card').find('.modal_order').attr('data-id'));
        let qty=parseInt($('input[name=inputModal]').val());
        let username=$('input[name=name]').val();
        let email=$('input[name=email]').val();
        let phone=$('input[name=phone]').val();

        $.ajax( {
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }

                ,
                url: '/modal/place-order',
                type: 'POST',
                dataType: 'JSON',
                data: {
                    id:id,
                    qty:qty,
                    username:username,
                    email:email,
                    phone:phone
                }

                ,
                success: function(response) {
                    $('#staticBackdrop').modal('hide');
                    $('.notif_text').html(response.notif_text);

                    setTimeout(function () {
                            $('.toast').toast('show');
                        }

                        , 250);
                }
            }

        );
        return false;
    }

);

购物车控制器:

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
                )
        )

)

当我尝试这样做时:

$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
                )

            [totalQty] => Something
            [totalPrice] => Something
        )

)

如何将这些数据从数组中取出,但将其留在对象中?

【问题讨论】:

    标签: php arrays laravel


    【解决方案1】:

    您将在与您的“$request->id”相同的“级别”中添加totalQtytotalPrice。 你应该得到你的预期结果:

    $selprod['totalQty'] = Something ;
    $selprod['totalPrice'] = Something ;
    

    【讨论】:

      猜你喜欢
      • 2016-02-05
      • 1970-01-01
      • 1970-01-01
      • 2020-02-05
      • 2018-05-29
      • 1970-01-01
      • 1970-01-01
      • 2015-04-06
      • 1970-01-01
      相关资源
      最近更新 更多