I'm trying to convert a small JSON to PHP Associative Arrays, but I'm missing a simple thing. Please check the following:
JSON to convert:
$data = '{
"intent": "CAPTURE",
"purchase_units": [{
"amount": {
"currency_code": "EUR",
"value": "211.00",
}
}],
}';
My wrong PHP Associative Arrays:
$data = array(
"intent" => "CAPTURE",
"purchase_units" => array(
"amount" => array(
"currency_code" => "EUR",
"value" => "211.00",
),
),
);
My PHP code doesn't let me pass this JSON, there must be a small thing I'm missing. Please could you help?
Thank you very much!
CodePudding user response:
The value of purchase_amounts
should be an array of associative arrays.
$data = array(
"intent" => "CAPTURE",
"purchase_units" => array(array(
"amount" => array(
"currency_code" => "EUR",
"value" => "211.00",
),
)),
);
Since PHP allows you to use []
notation for array literals, the simplest way to do this is to just copy the JSON. Replace {}
with []
and :
with =>
$data = [
"intent" => "CAPTURE",
"purchase_units" => [[
"amount" => [
"currency_code" => "EUR",
"value" => "211.00",
]
]],
];
CodePudding user response:
You need to remove unwanted commas at end of purchase_units & amount value
Example
$data = '{
"intent": "CAPTURE",
"purchase_units": [{
"amount": {
"currency_code": "EUR",
"value": "211.00"
}
}]
}';
$res = json_decode($data, true);
print_r($res);