我在php中生成了如下数组:
array_push($json, (object)[
'location_lat' => $spot_lat,
'location_lng' => $spot_lng,
'cat' => $spot_category,
'additional' => $spot_additional
]);这个代码在我的“while”循环中。在我的功能结束时,我要做的是:
$fp = fopen('my-path/spots.js', 'w');
$addressPoints = json_encode($json);
fwrite($fp, $addressPoints);
fclose($fp);所有的工作都很好,我得到了一个很好的spots.js文件,如下所示:
[{"location_lat":"49.6149366","location_lng":"22.2754669","cat":"na-dziko","additional":["is_verified","paid","bike","fishing","pitch","shop","river","lake","forest","buildings","playground","sanitary","wifi","barbecue","kitchen","year","electricity"]},{"location_lat":"49.4257138","location_lng":"20.4740524","cat":"kempingi-i-pola-namiotowe","additional":["paid","bike","trails","river","forest","mountains","sanitary","barbecue","kitchen","electricity"]},{"location_lat":"44.52625","location_lng":"1.71944","cat":"kempingi-i-pola-namiotowe","additional":["paid","bike","pool","fishing","water_sport","trails","climber","6","Array","shop","river","forest","mountains","4","Array","sanitary","wifi","kitchen","restaurant","electricity","5","Array"]}]但问题是,我不知道如何将这个数组设置为变量?我需要生成这样的.js文件:
var myCustomVariable = [{"location_lat":"49.6149366","location_lng":"22.2754669","cat":"na-dziko","additional":["is_verified","paid","bike","fishing","pitch","shop","river","lake","forest","buildings","playground","sanitary","wifi","barbecue","kitchen","year","electricity"]},{"location_lat":"49.4257138","location_lng":"20.4740524","cat":"kempingi-i-pola-namiotowe","additional":["paid","bike","trails","river","forest","mountains","sanitary","barbecue","kitchen","electricity"]},{"location_lat":"44.52625","location_lng":"1.71944","cat":"kempingi-i-pola-namiotowe","additional":["paid","bike","pool","fishing","water_sport","trails","climber","6","Array","shop","river","forest","mountains","4","Array","sanitary","wifi","kitchen","restaurant","electricity","5","Array"]}]解决办法可能很简单,但我做不到。
发布于 2021-04-20 16:08:48
$addressPoints是一个字符串。因此,您可以简单地向其添加所需的var声明:
$addressPoints = 'var myCustomVariable = ' . json_encode($json);https://stackoverflow.com/questions/67182224
复制相似问题