Reputation: 459
I have this array
$params_array['application_id'] = 'xxxxxxxxx';
$params_array['v'] = 1.20;
$params_array['src'] = 'http://www.google.com/logos/2011/yokoyama11-hp.jpg';
$params_array['functions']['name'] = 'blur';
$params_array['functions']['params']['radius'] = '0.0';
$params_array['functions']['params']['sigma'] = '2.0';
$params_array['functions']['save']['image_identifier'] = 'MY_CLIENT_ID';
I need to transform it into json.
So I am doing this:
$json = json_encode($params_array,JSON_UNESCAPED_SLASHES);
The result is
{"application_id":"xxx","v":1.2,"src":"http://www.google.com/logos/2011/yokoyama11-hp.jpg","functions":{"name":"blur","params":{"radius":"0.0","sigma":"2.0"},"save":{"image_identifier":"MY_CLIENT_ID"}}}
but, the receiver API of that json wants it to be formed slightly different, like this:
{"application_id":"xxx","v":1.2,"src":"http://www.google.com/logos/2011/yokoyama11-hp.jpg","functions":[{"name":"blur","params":{"radius":"0.0","sigma":"2.0"},"save":{"image_identifier":"MY_CLIENT_ID"}}]}
The difference: after "functions": there is this bracket [, and it's closed at the end.
PHP somehow does not create the json with this bracket.
How can I get PHP to create the json with those brackets?
The receiver API is http://www.blitline.com/docs/quickstart
Upvotes: 1
Views: 83
Reputation: 493
You can try to use the flag JSON_FORCE_OBJECT
if you have PHP 5.4.0 or later :
$json = json_encode($params_array,JSON_UNESCAPED_SLASHES + JSON_FORCE_OBJECT);
Upvotes: 0
Reputation: 2172
Alright, run this example then you'll know how to achieve it.
$params_array = array();
$params_array['application_id'] = 'xxxxxxxxx';
$params_array['another_array'] = array("A","B","C");
echo json_encode($params_array );
Result: {"application_id":"xxxxxxxxx","another_array":["A","B","C"]}
Upvotes: 0
Reputation: 43479
So receiver expects functions
to be array objects, but you pass single object instead. Change $params_array['functions']['name']
to $params_array['functions'][$functionIndex]['name']
Upvotes: 1
Reputation: 861
Try this
$params_array['application_id'] = 'xxxxxxxxx';
$params_array['v'] = 1.20;
$params_array['src'] = 'http://www.google.com/logos/2011/yokoyama11-hp.jpg';
$function_array['name'] = 'blur';
$function_array['params']['radius'] = '0.0';
$function_array['params']['sigma'] = '2.0';
$function_array['save']['image_identifier'] = 'MY_CLIENT_ID';
$params_array['functions'] = array($function_array);
Reason:
JSON_ENCODE
will treat an Array as JSON OBJECT
if it is like Key=>Value
and it will treat as json array
if it is index based like [0] => value
Upvotes: 0
Reputation: 13725
You should prepare your structure this way:
$params_array['application_id'] = 'xxxxxxxxx';
$params_array['v'] = 1.20;
$params_array['src'] = 'http://www.google.com/logos/2011/yokoyama11-hp.jpg';
$params_array['functions'][0]['name'] = 'blur';
$params_array['functions'][0]['params']['radius'] = '0.0';
$params_array['functions'][0]['params']['sigma'] = '2.0';
$params_array['functions'][0]['save']['image_identifier'] = 'MY_CLIENT_ID';
(Making functions to a number indexed array.)
Upvotes: 2