variations=[
{"property_id":504, "value":"8", "is_available":true, "price": 100},
{"property_id":504, "value":"12", "is_available":true, "price": 105},
{"property_id":513, "value":"Pepperoni", "is_available":true},
{"property_id":513, "value":"Mushrooms", "is_available":true}
]&diameter_scale=341&custom_property_names={"513":"Topping"}
如何将我的数组转换为此类型的json
$insert_variation_data = array(
'property_id' => $_POST["property_id"][$ix],
'value' => $_POST["values"][$ix],
'is_available' => true,
'scale' => $_POST["scale"][$ix],
'price' => $_POST["price"][$ix]
);
我有阵列然后如何隐藏在上面的变化但我认为上面提到的json无效但我想要相同的转换
答案 0 :(得分:0)
试试这个,创建一个多维数组。
$insert_variation_data = array(
array(
'property_id' => $_POST["property_id"][$ix],
'value' => $_POST["values"][$ix],
'is_available' => true,
'scale' => $_POST["scale"][$ix],
'price' => $_POST["price"][$ix]
)
);
和
echo json_decode($insert_variation_data);
答案 1 :(得分:0)
@kanishka给出了正确的答案,但我们已经使用了endcode。例如,假设我的控制器中有一个功能测试,如下面的
function test()
{
$insert_variation_data = array(
'property_id' => 'x',
'value' => 'y',
'is_available' => true,
'scale' => 'z',
'price' => 'm'
);
$insert_variation_data[] = array(
'property_id' => 'a',
'value' => 'b',
'is_available' => true,
'scale' => 'c',
'price' => 'd'
);
print_r(json_encode(array($insert_variation_data)));
}
这会给我们一个输出
[{"property_id":"x","value":"y","is_available":true,"scale":"z","price":"m","0":{"property_id":"a","value":"b","is_available":true,"scale":"c","price":"d"}}]
如果你想在ajax或其他方面做出此回应。请在必须使用的地方添加这些代码,以便我们提供更多帮助。