I'm creating a function to export a JSON to a specified format. I need to include a square bracket within a single element. As you can see "details_order" contain a single element with curly brackets, i need to add an additional square brackets into it. Please advise, thanks

original code:

[ { "reference": "20190531", "orders": [ { "id": "12345", "label": "22776", "address": "ABC, Apple road", "details_orders": { "ref": "AB07-332C" } } ] } ]

expected code:

[ { "reference": "20190531", "orders": [ { "id": "12345", "label": "22776", "address": "ABC, Apple road", "details_orders": [ { "ref": "AB07-332C" } ] } ] } ]

1 Answers

0
user166673 On

When you encode your data structure into JSON format, just make sure the "ref" value is stored in an array inside the "details_orders" array.

Here's an example of how the data should be structured in PHP to output the desired JSON:

 Array
(
    [0] => Array
        (
            [reference] => 20190531
            [orders] => Array
                (
                    [0] => Array
                        (
                            [id] => 12345
                            [label] => 22776
                            [address] => ABC, Apple road
                            [details_orders] => Array
                                (
                                    [0] => Array
                                        (
                                            [ref] => AB07-332C
                                        )

                                )

                        )

                )

        )

)

Here is how you could initialise such an array in PHP:

$data = [
    [
        "reference" => "20190531",
        "orders" => [
            [
                "id" => "12345",
                "label" => "22776",
                "address" => "ABC, Apple road",
                "details_orders" => [
                    [
                        "ref" => "AB07-332C"
                    ]
                ]
            ]
        ]
    ]
];