Search code examples
phpjsonpannellum

How to get rid of the "{" and "}" in json?


I'm working on the map hotspots. Which are needs to correct exactly to the manual. But from my code. There're more character interfere the plugin interpret.

This is my json:

[{//this is the extra
  "hs1": {
    "title": "Door Way",
    "hfov": "110",
    "pitch": "-3",
    "yaw": "130",
    "type": "equirectangular",
    "panorama": "\/photos\/Door-Way-5a57aff9c30b7.jpg",
    "hotSpots": [{
      "id": "17",
      "pitch": "-0.8069389",
      "yaw": "-174.40953",
      "type": "info",
      "text": "Closet"
    }]
  }
},//extra
{//extra
  "hs2": {
    "title": "Toilet",
    "hfov": "110",
    "pitch": "-3",
    "yaw": "130",
    "type": "equirectangular",
    "panorama": "\/photos\/Toilet-5a58b8ed2cb23.jpg",
    "hotSpots": ""
  }
}//extra]

In every set of new glossary, there is an extra "{}" coming in.

This is my PHP:

$result[]=array(
    "hs".$rec_scnArr['id']=>
    array(
    "title"=>$rec_scnArr['title'],
    "hfov"=>$rec_scnArr['hfov'],
    "pitch"=>$rec_scnArr['pitch'],
    "yaw"=>$rec_scnArr['yaw'],
    "type"=>"equirectangular",
    "panorama"=>"/photos/".$rec_sIMG['pic_file'],
    "hotSpots"=>$hotSpots           //$hotSpots is the array
    )
);

echo json_encode($result,true);

I'm sure there must be something wrong with the php loop. But I cannot see. This is the example from the plugin page.

So the question is how to get rid of the extra "[" and "{" in the json code? I think it's doesn't matter but it is. When I put the raw code in there. It's working. Then I think this is the problem.


Solution

  • It looks like what you want is a single object with multiple properties instead of an array of objects each having a single property.

    To achieve this you need to add all the properties to the same array:

    $result["hs".$rec_scnArr['id']] = array(
        "title"=>$rec_scnArr['title'],
        "hfov"=>$rec_scnArr['hfov'],
        "pitch"=>$rec_scnArr['pitch'],
        "yaw"=>$rec_scnArr['yaw'],
        "type"=>"equirectangular",
        "panorama"=>"/photos/".$rec_sIMG['pic_file'],
        "hotSpots"=>$hotSpots           //$hotSpots is the array
    );
    
    echo json_encode($result,true);