Search code examples
javascriptphpjsonpannellum

PHP array to JSON having a parameter with a callback function


On my PHP script, I have an array similar to this:

$panorama = array(
    "default" => array(
        "firstScene" => 2,
        "author" => 'Felipe'
    ), 
    "scenes" => array(
        "circle" => array(
            "title" => "Title 1", 
            "hotSpots" => array(
                "pitch" => "-2.1",
                "createTooltipFunc" => "hotspot"
            )
        )
    )
);

This array will be passed to a Javascript funcion, as a json object, like this:

pannellum.viewer('panorama', <?php echo json_encode($panorama); ?>);

The parameter createTooltipFunc have to receive a callback function named hotspot. So, in the final json, when I pass this PHP array to the Javascript function, this parameter should be like this: "createTooltipFunc" : hotspot, without the double quotes. How do I do that?

Just to give more information, I'm trying to create a tour using the plugin pannellum.js and I'm geting all the info of the json I need from my mysql DB.


Solution

  • Keep a Javascript variable in between which keeps the object and update the property before it's using with pannellum(Assumes hotspot is a variable).

    //  create Javascript object
    var obj = <?php echo json_encode($panorama); ?>;
    
    // update createTooltipFunc proeprty with the variable
    // where variable name can be extract from $panorama
    obj.scenes.circle.hotSpots.createTooltipFunc = <?php echo $panorama['scenes']['circle']['hotSpots']['createTooltipFunc']; ?>;
    
    pannellum.viewer('panorama', obj);