将格式化为数组的字符串转换为实际数组


Convert a string formatted as an array TO a real array

我有一个POST Rest API,它有一个名为$arrProducts的参数。参数应该是一个数组。但是发送给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解析数据的方式:这是我写的,但由于某种原因,有一个问题在选项数组。

$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数组。

就这样做

$arrProducts = array("arrProducts"=>array(whatever));
$dataToPost = json_encode($arrProducts);
现在在您的REST代码中,
$stringVar = $_POST['arrProducts'];
$arrProducts = json_decode($stringVar);

so $arrProducts包含数组格式的数据

如果您必须将数组作为字符串传递,我建议您使用php的json_encodejson_decode方法在REST路由上传递JSON字符串,但在需要的地方有一个数组。

根据问题的变化更新:

你的代码中有这一行

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

但是,用于描述数组格式的代码不会在$arrProductsVar

中创建arrProducts键。

foreach ($arrProductsVar as $arrProduct){