1

First, im a beginner in PHP so please bear with me.

I want to create something like this JSON array :

{
children: [
{
id: "1"
user_id: "1"
name: "Test"
gender: "M"
born_date: "2014-10-01"
born_hour: "12:12:12"
hospital: "test hospital"
}-
}-
{
id: "2"
user_id: "1"
name: "test2"
gender: "F"
born_date: "2014-10-06"
born_hour: null
hospital: null
}-
]
result: "true"
total: 2
}

To make my question easier, this is an easy to read JSON i want to create (you can think mobiles as children in my case) :

enter image description here

The children should become the parent and array. This is what i accomplished so far :

{
0: {
children: {
id: "1"
user_id: "1"
name: "Test"
gender: "M"
born_date: "2014-10-01"
born_hour: "12:12:12"
hospital: "test hospital"
}-
}-
1: {
children: {
id: "2"
user_id: "1"
name: "test2"
gender: "F"
born_date: "2014-10-06"
born_hour: null
hospital: null
}-
}-
result: "true"
total: 2
}

The children is not the parent of the items and not in array ([]).

This is my code to generate the above JSON :

$resultArray = array('result' => 'true', 'total' => $size);

            foreach($result as $child)
            {
                if($child)
                {
                    //add to array
                    $resultArray[] = array('children' => array('id' => $child['id'], 
                    'user_id' => $child['user_id'], 
                    'name' => $child['name'], 
                    'gender' => $child['gender'],
                    'born_date' => $child['born_date'], 
                    'born_hour' => $child['born_hour'],
                    'hospital' => $child['hospital']));
                }
            }

            $this->response($resultArray, 200);

Please kindly help me. Thanks for your help.

2 Answers 2

1

You could just buil the array first, push all items etc. Then finally put that parent key on the end and put the gathered array inside that:

foreach($result as $child) {
    if($child) {
        // so build it first
        $resultArray[] = array(
            'id' => $child['id'], 
            'user_id' => $child['user_id'], 
            'name' => $child['name'], 
            'gender' => $child['gender'],
            'born_date' => $child['born_date'], 
            'born_hour' => $child['born_hour'],
            'hospital' => $child['hospital']
        );
    }
}
// end then finally, put it inside the parent key children
$resultArray = array('result' => 'true', 'total' => $size, 'children' => $resultArray);
$this->response($resultArray, 200);
Sign up to request clarification or add additional context in comments.

3 Comments

Im trying this now. Thanks
@BlazeTama okay no prob, i hope this does well for you
Thanks a lot! I will accept your answer in 4 mins (the system's rule)
1

You can use 'json_encode' function.

$resultArray = json_encode($result);

http://php.net/manual/en/function.json-encode.php

1 Comment

Sorry i think we have some miss-understanding. The JSON has benn encoded. Thanks anyway

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.