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

Time:12-25

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.

CodePudding user response:

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;

CodePudding user response:

Your $sides variable does not contain an array with multiple entities but just one "dictionary" (one JSON object). If you e.g. add a loop around, it should work:

<?php
// loop over whatever generates the sides
$sides_array = [];

$sides_array['id'] = 1;
$sides_array['name'] = 'Frontside';
$sides_array['name_nl'] = 'Voorkant';
$sides_array['template_overlay'] = $templateoverlay;

if(empty($uploadarray['product_data']['sides'])){
    // initialize "sides"
    $uploadarray['product_data']['sides'] = [];
}

$uploadarray['product_data']['sides'][] = $sides_array;
?>
  • Related