首页 > 解决方案 > 数组:如何实现这种数组

问题描述

美好的一天,我正在为我的学习做一个购物车订单服务API。我一直在努力解决如何检查多个项目。

第一个问题

我不知道如何将数组(如下面的输出)从发布请求传递到我的控制器或方法。在用户完成选择他/她的项目之后。

第二

我怎样才能实现一个数组(如下面的输出

当用户 1234 选择 1 个项目时,输出将如下所示。

Array ( [user_id] => 1234 
        [0] => Array ( 
                        [product_id]    => 123 
                        [product_name]  => sample product 1 
                        [product_desc]  => A sample product 
                        [product_price] => 100 
                    ) 
        )

当用户 1234 选择 2 个项目时。这将是输出。

Array ( [user_id] => 1234 
        [0] => Array ( 
                        [product_id]    => 1 
                        [product_name]  => sample product 1 
                        [product_desc]  => A sample product 
                        [product_price] => 100 
                    ) 
        [1] => Array ( 
                        [product_id]    => 2 
                        [product_name]  => sample product 2 
                        [product_desc]  => A sample product 
                        [product_price] => 100 
                    ) 
        )

当用户 12 选择 2 个项目时。这将是输出。

Array ( [user_id] => 12 
        [0] => Array ( 
                        [product_id]    => 1
                        [product_name]  => sample product 2 
                        [product_desc]  => A sample product 
                        [product_price] => 100 
                    ) 
        [1] => Array ( 
                        [product_id]    => 2 
                        [product_name]  => sample product 2 
                        [product_desc]  => A sample product 
                        [product_price] => 200 
                    ) 
        )

等等...

编辑这是我的测试结帐表格。目前它一次只能结帐单个项目。

<form action="/test/api/checkoutItem" method="POST">
    <input type="hidden" name="cart_items_id" id="cart_items_id" value="55">
    <input type="hidden" name="user_id" id="user_id" value="11647748">
    <input type="hidden" name="product_id" id="product_id" value="70">
    <input type="input"  name="qty" id="qty">
</form>

控制器

public function checkoutItem(Request $request)
{ 
    $id     = $request->user_id;
    //Status TESTED AND OK.
    try{
        //Check if cart has an item whith status id 1 (Added).
        $cart = \DB::table('cart_items')
                ->whereRaw('cart_items.id = ' .$request->cart_items_id. 
                    ' AND cart_items.product_id = ' .$request->product_id. 
                    ' AND cart_items.status_id = 1')->count();

        if($cart != NULL){

            Stripe::setApiKey( config('services.stripe.secret') );
            $student= \App\User::find($id);

            //Get the product details using the product_id
            $product= \App\Product::find($request->product_id);

            //Check if student's has no stripe id.
            if($student->stripe_id == NULL){
                //create stripe id for student.
                $customer= Customer::create([
                                            'email'    => request('stripeEmail'),
                                            'source'   => request('stripeToken')
                                            ]);
                //Update stripe_id from null to newly created stripe_id generated by stripe.
                $user= \App\User::where('id', $id)
                ->update( array('stripe_id' => $customer->id) ); 

                //Charge the student.
                Charge::create([
                    'customer'      => $customer->id,
                    'description'   => $product->description,
                    'amount'        => ($product->price * 100) * $request->qty, //Should be in cent.
                    'currency'      => 'aud',
                    'receipt_email' => $request->stripeEmail,
                    'metadata'      => array(
                                            "product_id"        => $request->product_id, 
                                            "product_name"      => $product->name,
                                            "product qty"       => $request->qty,
                                            "product_type_id"   => $product->product_type_id,
                                            "sub_type_id"       => $product->service_sub_type_id,
                                            "user_id"           => $id,
                                            "fullname"          => $student->first_name." ".$student->last_name,
                                            "email"             => $request->stripeEmail
                                            )
                    ]);
            //If student's has already stripe id, create a charge.   
            }else{
                //Charge
                Charge::create([
                    'customer'      => $student->stripe_id,
                    'description'   => $product->description,
                    'amount'        => ($product->price * 100) * $request->qty, //Should be in cent.
                    'currency'      => 'aud',
                    'receipt_email' => $student->email,
                    'metadata'      => array(
                                            "product_id"        => $request->product_id, 
                                            "product_name"      => $product->name,
                                            "product qty"       => $request->qty,
                                            "product_type_id"   => $product->product_type_id,
                                            "sub_type_id"       => $product->service_sub_type_id,
                                            "user_id"           => $id,
                                            "fullname"          => $student->first_name." ".$student->last_name,
                                            "email"             => $student->email
                                            )
                    ]); 
            }

            //If successfully charge, status_id wll be updated from 1(Added) to 2(Checkout).
            $upd_cart_items= \DB::table('cart_items')
            ->leftjoin('carts', 'cart_items.cart_id', '=', 'carts.id')
            ->whereRaw('cart_items.id = ' .$request->cart_items_id)
            ->update(['cart_items.quantity' => $request->qty,'cart_items.status_id' => 2, 
                      'cart_items.updated_at' => Carbon::now()->toDateTimeString()]);

            return ['success' => true, 'message' => "Product was successfully purchased"];

        }
    }catch(\Stripe\Error\Card $e){
        return['error' => $e];
    }
}

抱歉问了这个愚蠢的问题。

但是有人说:

问一个问题的人傻了五分钟;不问问题的人永远是个傻瓜

标签: phparrayslaravel-5.2

解决方案


PHP 处理 POST 和 GET 变量的一个特点是它自动解码索引表单变量名称。

你可以这样做

<form ....>
<input name="person[userId]" value="1234" />
<input name="person[0][product_id]" value="1" />
<input name="person[0][product_name]" value="sample product 1" />
...
<input name="person[1][product_id]" value="2" />
<input name="person[1][product_name]" value="sample product 2" />
</form>

在 php 中,您可以通过以下方式检索它:

<?php
var_dump($_POST['person']);
//will get you something like:
    [user_id] => 1234
    [0] => Array ( 
              [product_id]    => 1
              [product_name]  => sample product 1 
          ) 
    [1] => Array ( 
              [product_id]    => 2 
              [product_name]  => sample product 2 
          ) 
?>

推荐阅读