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!
>Solution :
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",
]
]],
];