How to wrap part of my JSON with brackets using PHP? -- [Question Asked]

Issue

I have some JSON that looks like this:

{
   "order_id":"59.1595",
   "quantity":"1",
   "orderline":"61b9f15a158ee",
   "customer_id":"59",
   "product_thumbnail":"https:\/\/website.nl\/cms\/images\/producten\/deelpaneel\/afbeeldingen\/_deelpaneel_foto_op_rvs.jpg",
   "rulers":"cm",
   "product_data":{
      "id":"custom",
      "dpi":"50",
      "name":"Deelpaneel",
      "size":"1000x550",
      "bleed":"10",
      "sides":{
         "id":1,
         "name":"Frontside",
         "name_nl":"Voorkant",
         "template_overlay":"https:\/\/website.nl\/cms\/images\/producten\/deelpaneel\/templates\/2_deelpaneel_100x55cm1cmafstandhouders.svg"
      }
   },
   "safety":"",
   "has_contour":"false",
   "preview_link":"",
   "redirect_link":"https:\/\/website.nl\/winkelwagen",
   "procheck":"n"
}

I create it with PHP and use json_encode.

My question is how do I get brackets around the inside of sides?

Like in this example:

"sides": [
            {
                "id": 1,
                "name": "Frontside",
                "name_nl": "Voorkant",
                "template_overlay": null
            },
            {
                "id": 2,
                "name": "2 side en",
                "name_nl": "2 side nl",
                "template_overlay": null
            },
            {
                "id": 3,
                "name": "3 side en",
                "name_nl": "3 side nl",
                "template_overlay": null
            }
        ],
        "safety": 10,

This is how I create that part with PHP:

<?PHP
if(!empty($uploadarray['product_data']['sides'])){
    // Multiple sides
}else{
    $uploadarray['product_data']['sides']['id'] = 1;
    $uploadarray['product_data']['sides']['name'] = 'Frontside';
    $uploadarray['product_data']['sides']['name_nl'] = 'Voorkant';
    $uploadarray['product_data']['sides']['template_overlay'] = $templateoverlay;
}
?>

Then I create the entire JSON with: $json = json_encode($uploadarray);

I’ve read that you need to wrap it in another array but I can’t get it to work.

For example:

array(array($uploadarray['product_data']['sides']['id'] = 1));

Or

array($uploadarray['product_data']['sides']['name'] = 'Frontside');

Just output the same json result.

Answer we found from sources

First create your array

$side = [
    'id'               => 1,
    'name'             => 'Frontside',
    'name_nl'          => 'Voorkant',
    'template_overlay' => $templateoverlay
];

Then, add it :

// Check this -----------------------vv
$uploadarray['product_data']['sides'][] = $side;

Answered By – Cid

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Posted in PHP

Who we are?

We are team of software engineers in multiple domains like Programming and coding, Fundamentals of computer science, Design and architecture, Algorithms and data structures, Information analysis, Debugging software and Testing software. We are working on Systems developer and application developer. We are curious, methodical, rational, analytical, and logical. Some of us are also conventional, meaning we're conscientious and conservative.

Answer collected from stackoverflow and other sources, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0