0

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"}"
4
  • 1
    Please show us your php array $children Commented Nov 5, 2018 at 9:02
  • "I convert to a JSON object then to a JSON string"...this make no sense. JSON is just a string. It's decodable into objects etc, but as soon as you make it into JSON, you already have a string. When you commented //Convert the PHP array to a JSON object you'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. Commented Nov 5, 2018 at 9:02
  • @ADyson Thanks,, duly noted Commented Nov 5, 2018 at 9:23
  • @HarveyFletcher This my my children array ['child_name' => $childname , 'child_dob' => $childdob] Commented Nov 5, 2018 at 9:24

1 Answer 1

3

No need to convert it to object. Try this instead

$data=array();
$data['children'] =$children;
//Convert JSON object to a JSON string to send to server
$ch = json_encode($data);

dd($ch);
Sign up to request clarification or add additional context in comments.

4 Comments

Hi,, I have implemented your solution and I get the following data after dd() "{"children":{"child_name":"mnmbmb","child_dob":"2018-10-30"}}" This is still throwing error with the API which expects children property to contain an array of objects as shown below "children":[ {"child_name":"abc","child_dob":"2015-05-23"}, {"child_name":"efg","child_dob":"2016-09-13"} ] format
Your $children array as you have shown above contains only one row but it should contain an array of values or rows.
I have checked the documentation and it can accept 1 row,, the data am sending is a JSON string, how do I convert it back to a JSON object which is the format that the API requires
By array of values or rows I meant that your $children array must be a 2d array. For example try using this array as $children: array(array('child_name'=>'abc','child_dob'=>'2015-05-23'),array('child_name'=>'efg','child_dob'=>'2016-09-13'))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.