0

$arrProducts というパラメーターを持つ POST Rest API があります。パラメータは配列でなければなりません。ただし、API に送信される値は配列ではなく、配列の形式の文字列です。

渡される文字列パラメーターの例を挙げましょう。

"$arrProducts = array(array("product_id"=>'79',"qty"=>2,"options" =>array("525"=>'')),array("product_id"=>'41',"qty"=>3),"options"=>array("195"=>'')));"

配列のように見えますが、このパラメータはそうではありません。文字列です。だから私はそれをもっと配列のようにしようとしました。

$stringVar = $_POST['arrProducts'];
$str = rtrim($stringVar, ";"); //To lose the semicolon at the end of the string
$arrProducts = substr($str, 15); // To lose the first part of the string "$arrProducts = " to make it formatted exactly like an array.

この後、純粋な配列形式になりました"array(array("product_id"=>'79',"qty"=>2,"options" =>array("525"=>'')),array("product_id"=>'41',"qty"=>3),"options"=>array("195"=>'')))"

そして今、私の質問は、この文字列を配列に変換するにはどうすればよいですか?

私がしたことは、データがRest APIに送信される方法を最初に変更することでした。json_encode を使用しました。次に、Rest API で、json_decode を使用してデータを取得しました。

これが私が持っている新しい問題です。arrProducts の形式は次のとおりです。json_decode の考えの解析に問題があります。

$product_id = "45";
        $qty = 20;
        $option_type_id_for_option_id_151 = '826';
        $option_type_id_for_option_id_124 = '657,658,';
        $option_type_id_for_option_id_126 = 'Test for field option';

    $arrProducts = array(
        array(
            "product_id" => $product_id,
            "qty" => $qty,
            "options" => array(         
                "151" => $option_type_id_for_option_id_151,
                "124" => $option_type_id_for_option_id_124,
                '126' => $option_type_id_for_option_id_126
            )
        ),
        array(
            "product_id" => '60',
            "qty" => '1',
            "options" => array(         
                "156" => '862',
                "167" => '899',
                "168" => '902',
                "159" => '877',
                "160" => '889,890,891,'
            )
        ),
        array(
            "product_id" => '58',
            "qty" => '1',
            "options" => array(         
                "174" => '938',
                "176" => '943',
                "178" => ''
            )
        )

    );

問題は、json_decode を使用してデータを解析する方法にあります。これは私が書いたものですが、何らかの理由で options 配列に問題があります。

$stringVar = $_POST['arrProducts'];
$arrProductsVar = json_decode($stringVar, TRUE);
$i = 0;
        $arrProducts = array();
        if ($arrProductsVar !== NULL)
        { 

            foreach ($arrProductsVar['arrProducts'] as $arrProduct){
                $options = array();
                foreach($arrProduct['options'] as $key => $val){
                     $options[$key] = $val;
                }

                $arrProducts[$i] = array('product_id' => $arrProduct['product_id'],'qty' => $arrProduct['qty'], 'options' => $options);
                $i++;
            }

        }

このコードに古典的なエラーが見られる人はいますか? なんらかの理由で機能していないためです。おそらく $options 配列が原因です。うまく整形できていないと思います。

4

2 に答える 2

0

配列を文字列として渡す必要がある場合は、phpjson_encodejson_decodeメソッドを使用して REST ルートで JSON 文字列を渡すことをお勧めしますが、必要な場所には配列を用意してください。

質問への変更に基づいて更新:

コードにこの行があります

foreach ($arrProductsVar['arrProducts'] as $arrProduct){

ただし、配列形式を記述するために使用するコードは、配列にarrProductsキーを作成しません。$arrProductsVar

試す

foreach ($arrProductsVar as $arrProduct){
于 2013-04-24T08:49:58.810 に答える