0

I have a JSON String. I want it to be filled in Array. Here is Array which i receive.

My JSON Response is:

{
    "model": "SyncData",
    "unique_id": "c12fb356f90d032b",
    "key": "sdjvnsdivbsnd",
    "sync_data": {
        "array_a": [{
            "a_fav": "true",
            "a_id": 1
        }, {
            "a_fav": "false",
            "a_id": 2
        }],
        "array_b": [{
            "b_fav": "false",
            "b_id": 8
        }],
        "c_array": [{
            "c_fav": "false",
            "c_id": 15996
        }],
        "patient_list_array": [{
            "unique_id": "sdvsdvsdvdsdv",
            "p_status": "false",
            "p_id": 1454943805215,
            "p_note": "2",
            "p_code": "8",
            "p_timestamp": 1454943805216,
            "p_name": "ABC XYZ",
            "p_status": 1,
            "p_room_no": "5"
        }],

        "array_d": [{
            "d_assigned_id": "30",
            "d_fav": "true"
        }]
    }
}

I want to store all this data in Array and from that in DataBase.

5
  • 3
    Use json_decode($string, true) also this is an object, which contains other properties that are arrays. I suggest you leave it as an object so dont use the second parameter i.e. ,true Commented Feb 9, 2016 at 10:59
  • if just in array format json_decode($string, true) , if remove second param u will get the result in object form. Commented Feb 9, 2016 at 11:06
  • To be clear, your talking about transforming JSON into native PHP data types. Commented Feb 9, 2016 at 11:34
  • Ya. I ma talking about transforming JSON into native PHP. Since i am learning Web Services may be my words are not proper. Commented Feb 9, 2016 at 12:23
  • U didn't get the solution or given answers not valid? Commented Feb 9, 2016 at 21:10

2 Answers 2

1

If you want this json string in an array than you can use

json_decode($string,true);

Note that, second param of json_decode will return the array if you need result in object form than remove the second param "true".

Your Code:

$string = '{
    "model": "SyncData",
    "unique_id": "c12fb356f90d032b",
    "key": "sdjvnsdivbsnd",
    "sync_data": {
        "array_a": [{
            "a_fav": "true",
            "a_id": 1
        }, {
            "a_fav": "false",
            "a_id": 2
        }],
        "array_b": [{
            "b_fav": "false",
            "b_id": 8
        }],
        "c_array": [{
            "c_fav": "false",
            "c_id": 15996
        }],
        "patient_list_array": [{
            "unique_id": "sdvsdvsdvdsdv",
            "p_status": "false",
            "p_id": 1454943805215,
            "p_note": "2",
            "p_code": "8",
            "p_timestamp": 1454943805216,
            "p_name": "ABC XYZ",
            "p_status": 1,
            "p_room_no": "5"
        }],

        "array_d": [{
            "d_assigned_id": "30",
            "d_fav": "true"
        }]
    }
}';

$array = json_decode($string,true);
echo "<pre>";
print_r($array);

Result:

Array
(
    [model] => SyncData
    [unique_id] => c12fb356f90d032b
    [key] => sdjvnsdivbsnd
    [sync_data] => Array
        (
            [array_a] => Array
                (
                    [0] => Array
                        (
                            [a_fav] => true
                            [a_id] => 1
                        )

                    [1] => Array
                        (
                            [a_fav] => false
                            [a_id] => 2
                        )

                )

            [array_b] => Array
                (
                    [0] => Array
                        (
                            [b_fav] => false
                            [b_id] => 8
                        )

                )

            [c_array] => Array
                (
                    [0] => Array
                        (
                            [c_fav] => false
                            [c_id] => 15996
                        )

                )

            [patient_list_array] => Array
                (
                    [0] => Array
                        (
                            [unique_id] => sdvsdvsdvdsdv
                            [p_status] => 1
                            [p_id] => 1454943805215
                            [p_note] => 2
                            [p_code] => 8
                            [p_timestamp] => 1454943805216
                            [p_name] => ABC XYZ
                            [p_room_no] => 5
                        )

                )

            [array_d] => Array
                (
                    [0] => Array
                        (
                            [d_assigned_id] => 30
                            [d_fav] => true
                        )

                )

        )

)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. Got the solution. With some improvement i can use this.
@AshishVora: glad to help bro
1

Simple code to use to visualise your JSON String

<?php
$string = '{
    "model": "SyncData",
    "unique_id": "c12fb356f90d032b",
    "key": "sdjvnsdivbsnd",
    "sync_data": {
        "array_a": [{
            "a_fav": "true",
            "a_id": 1
        }, {
            "a_fav": "false",
            "a_id": 2
        }],
        "array_b": [{
            "b_fav": "false",
            "b_id": 8
        }],
        "c_array": [{
            "c_fav": "false",
            "c_id": 15996
        }],
        "patient_list_array": [{
            "unique_id": "sdvsdvsdvdsdv",
            "p_status": "false",
            "p_id": 1454943805215,
            "p_note": "2",
            "p_code": "8",
            "p_timestamp": 1454943805216,
            "p_name": "ABC XYZ",
            "p_status": 1,
            "p_room_no": "5"
        }],

        "array_d": [{
            "d_assigned_id": "30",
            "d_fav": "true"
        }]
    }
}';
$obj = json_decode($string);
print_r($obj);

Which will output :

stdClass Object
(
    [model] => SyncData
    [unique_id] => c12fb356f90d032b
    [key] => sdjvnsdivbsnd
    [sync_data] => stdClass Object
        (
            [array_a] => Array
                (
                    [0] => stdClass Object
                        (
                            [a_fav] => true
                            [a_id] => 1
                        )

                    [1] => stdClass Object
                        (
                            [a_fav] => false
                            [a_id] => 2
                        )

                )

            [array_b] => Array
                (
                    [0] => stdClass Object
                        (
                            [b_fav] => false
                            [b_id] => 8
                        )

                )

            [c_array] => Array
                (
                    [0] => stdClass Object
                        (
                            [c_fav] => false
                            [c_id] => 15996
                        )

                )

            [patient_list_array] => Array
                (
                    [0] => stdClass Object
                        (
                            [unique_id] => sdvsdvsdvdsdv
                            [p_status] => 1
                            [p_id] => 1454943805215
                            [p_note] => 2
                            [p_code] => 8
                            [p_timestamp] => 1454943805216
                            [p_name] => ABC XYZ
                            [p_room_no] => 5
                        )

                )

            [array_d] => Array
                (
                    [0] => stdClass Object
                        (
                            [d_assigned_id] => 30
                            [d_fav] => true
                        )

                )

        )

)

Comments

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.