0

I have an array which i getting from an api, but its format is not correct.

Response Array

Array
(
    [route] => checkout/success
    [utf8] => ✓
    [req_bill_to_address_country] => AE
    [auth_avs_code] => 1

)

I'm getting this array through $_Request

print_r($_Request);

I want to convert it in this format.

Array
(
    'route' => 'checkout/success',
    'utf8' => '✓',
    'req_bill_to_address_country' => 'AE',
    'auth_avs_code' => 1

)

So how can i do this, I have tried with explode() function but its not converting properly.

adnan khalid
  • 201
  • 2
  • 3
  • 8

1 Answers1

1

you can use this code

$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);

The $res variable is your standard array and you can use this in your code

Amir Mohsen
  • 853
  • 2
  • 9
  • 23