Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to wrap part of my JSON with brackets using PHP?

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading