我有一个从api获取的数组,但其格式不正确。
Array
(
[route] => checkout/success
[utf8] => ✓
[req_bill_to_address_country] => AE
[auth_avs_code] => 1
)
我通过$_Request
print_r($_Request);
我想以这种格式转换它。
Array
(
'route' => 'checkout/success',
'utf8' => '✓',
'req_bill_to_address_country' => 'AE',
'auth_avs_code' => 1
)
那么我怎么能这样做,我尝试过explode()
函数,但它没有正确转换。
答案 0 :(得分:1)
您可以使用此代码
$str = "Array
(
[route] => checkout/success
[utf8] => ✓
[req_bill_to_address_country] => AE
[auth_avs_code] => 1
)";
$str = str_replace([" ",')','(','Array'],"",$str);
$array = explode("\n",$str);
$res = [];
foreach($array as $row){
if(!empty($row)){
$temp = explode(" => ",$row,2);
$res[str_replace(["[","]"],"",$temp[0])] = $temp[1];
}
}
echo "<pre>";
print_r($res);
$ res变量是您的标准数组,您可以在代码中使用它