I’m trying to get the values of a text area and create a json object based on that .
The textarea looks like this:
<textarea name="msisdn" wrap="physical">
51971855080
51971855081
51971855082
</textarea>
The dynamic parts are “messId” and “destAddr” the other ones will be replaced with php variables received from a form like this way:
$str_obj_json='{
"method":"SUBMIT","params":{
"batchType":"submit",
"batchId":"'.$batch.'",
"origAddr":"550",
"origTon":2,
"userData":"'.$sms.'",
"submits":
[
{
"messId":"mess127_009",
"destAddr":"51971855080"},
{
"messId":"mess127_010",
"destAddr":"51971855081"},
{
"messId":"mess127_011",
"destAddr":"51971855082"},
]
}
}';
How to create this part dynamically ?:
"submits":
[
{
"messId":"mess127_009",
"destAddr":"51971855080"},
{
"messId":"mess127_010",
"destAddr":"51971855081"},
{
"messId":"mess127_011",
"destAddr":"51971855082"},
]
Use
json_encode():It converts PHP arrays into JSON notation. So you can create your array dynamically as desired, and then encode it afterwards.
As for using the values from the
textarea: Just split the corresponding$_POSTdata into lines usingsplit("\n", $_POST['msisdn'])