Assign Json to php Variables -
i want make json details dynamic.currently static.as example want declare few php variables these json.
here code
$body = '{ "outboundsmsmessagerequest": { "address": [ "tel:+9456654978" ], "senderaddress": "tel:+95623654978", "outboundsmstextmessage": { "message": "welcome fgf confirmation code - " }, "clientcorrelator": "", "receiptrequest": { "notifyurl": "", "callbackdata": "" }, "sendername": "" } }';
as in here can see json has declared $body.what want make separate variables such $message,$address, $senderaddress , assign them $body. how can this?
initial json:
$body = '{ "outboundsmsmessagerequest": { "address": [ "tel:+9456654978" ], "senderaddress": "tel:+95623654978", "outboundsmstextmessage": { "message": "welcome fgf confirmation code - " }, "clientcorrelator": "", "receiptrequest": { "notifyurl": "", "callbackdata": "" }, "sendername": "" } }';
decode json array
i use second parameter true because need array associative.
$arr = json_decode($body, true);
now insert necessary value:
$arr['outboundsmsmessagerequest']['address'] = "tel:+1234567890"; $arr['outboundsmsmessagerequest']['senderaddress'] = "tel:+0987654321"; $arr['outboundsmsmessagerequest']['outboundsmstextmessage']['message'] = "test message";
now encode array json
$body = json_encode($arr);
output
print_r($body);
result
{"outboundsmsmessagerequest":{"address":"tel:+1234567890","senderaddress":"tel:+0987654321","outboundsmstextmessage":{"message":"test message"},"clientcorrelator":"","receiptrequest":{"notifyurl":"","callbackdata":""},"sendername":""}}
Comments
Post a Comment