Php 将格式化为数组的字符串转换为实数组
我有一个PostRESTAPI,它有一个名为$arrProducts的参数。 参数应该是数组。 但是发送到API的值不是数组,而是一个字符串,其形式为数组Php 将格式化为数组的字符串转换为实数组,php,arrays,string,Php,Arrays,String,我有一个PostRESTAPI,它有一个名为$arrProducts的参数。 参数应该是数组。 但是发送到API的值不是数组,而是一个字符串,其形式为数组 $stringVar = $_POST['arrProducts']; $str = rtrim($stringVar, ";"); //To lose the semicolon at the end of the string $arrProducts = substr($str, 15); // To lose the first pa
$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.
让我给出一个传递的字符串参数示例:
"$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.
因此,在这之后,我使用了纯数组形式“数组(数组(“产品id=>'79',“数量”=>2,“选项”=>数组(“525”=>”),数组(“产品id=>'41',“数量”=>3),“选项”=>数组(“195”=>”)”
现在我的问题是如何将这个字符串转换成数组
好的,听着,我所做的是首先改变数据传输到RESTAPI的方式。
我使用json_编码。
然后在我的RESTAPI中,我使用json_解码获取数据
这是我的新一期。
产品的格式如下所示。我在解析json_解码思想时遇到问题
$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数组。我认为它的格式不好。如果必须将数组作为字符串传递,我建议您使用php的
json_encode
和json_decode
方法在REST路由上传递json字符串,但在需要的地方使用数组
根据问题的更改进行更新:
代码中有这一行
foreach ($arrProductsVar['arrProducts'] as $arrProduct){
但是,用于描述数组格式的代码不会在$arrcroductsvar
试一试
就这么做吧
$arrProducts = array("arrProducts"=>array(whatever));
$dataToPost = json_encode($arrProducts);
现在在您的REST代码中
$stringVar = $_POST['arrProducts'];
$arrProducts = json_decode($stringVar);
因此,$arrProducts以数组格式包含您的数据。嗯,我不知道源代码有多可信。但是如果它是数组的有效PHP声明,
eval
将起作用-但对于未知代码,它应该是最后的手段。eval()将不会像目前那样起作用。您将引号混合得太多,因此无论如何都无法将其作为字符串传递给eval。你需要先把它清理干净。另外,应该避免评估。。。这不是一个好主意,尤其是在API输入上。顺便说一句,数组的格式是错误的,所以如果想要在字符串中包含有效的PHP,那么它就不起作用了。你的括号太多了。我手工编写了数组,因此可能会有一些错误,但作为参数传递的字符串(格式为数组)是有效的。这是胡说八道,请使用易于解码/编码的格式,例如JSON。如果你坚持使用这种格式,你必须编写你自己的解析器,这将使进程更复杂,而这甚至不是一个问题。不幸的是,我必须将它作为字符串传递,因为这个字符串是使用javascript逐渐创建的,以构造这个数组格式。因此,您建议将构造的字符串格式化为数组,并对其进行json_编码?这样行吗?不在同一范围内吗?我将尝试对字符串而不是数组进行json_编码。以这种格式将PHP数组构建为字符串似乎很奇怪,尤其是在JS中。JSON是Javascript对象表示法,因此在JS中构造JSON字符串并将其传递到PHP URL要比构建这个特定字符串容易得多。但是很明显,你需要控制JS来完成这项工作。是的,我已经告诉创建javascript的开发人员,javascript将参数发送到API,发送到参数$arrProducts,这个数组将具有我提到的上述格式。他所做的就是发送一个格式化为数组的字符串。所以现在我必须弄清楚如何将这个字符串转换成一个真正的数组。请您也用javascript编写第一部分,因为数据是从javascript代码发送的。如果您有一个javascript数组,请使用JSON.stringify(yourArray),然后通过ajax post或get发送。好的,我会尝试一下,我会让您知道的!我也愿意接受其他想法。如果您使用的是jquery,那么您可以试试。serialize()也可以。stringify起作用了吗?如果有关联数组,请确保声明var yourArray={};