I have got some data in PHP that I want to send to an API writen in JSON format. My data was a PHP array whereby I convert to a JSON object then to a JSON string so that I can send. But the API requires an array of objects as shown in the code below,, how can i write my code to fit its format
Requirements from the array writen in JSON format
{
"children":[
{"child_name":"abc","child_dob":"2015-05-23"},
{"child_name":"efg","child_dob":"2016-09-13"}
]
}
My PHP code
//Convert the PHP array to a JSON object
$child =(object)$children;
//Convert JSON object to a JSON string to send to server
$ch = json_encode($child);
dd($ch);
//result in browser
"{"child_name":"mnmbmb","child_dob":"2018-10-30"}"
$children//Convert the PHP array to a JSON objectyou're actually still turning it into a PHP object. It's not JSON until you encode it as a JSON string. You might think I'm nitpicking but it helps to use the right terminology, then everyone is clear about what you mean, and can be sure that you understand what you're doing.['child_name' => $childname , 'child_dob' => $childdob]