为 Javascript 对象删除 foreach 循环内的引号 - Laravel 5.4 - PHP
Remove quotes inside foreach loop for a Javascript object - Laravel 5.4 - PHP
我试图在地图上显示一些列表,但我无法在部分中显示 'targetSVG',因为它包含“”(引号)。我告诉你我的意思:
这是它现在在查看源代码部分的显示方式:
如您所见,"targetSVG" 显示引号,我需要将其删除。
这就是我在控制器中设置对象的方式:
$listingLocations = Location::all();
$locations = [];
foreach( $listingLocations as $location ){
$locations[] = ['svgPath' => 'targetSVG', 'zoomLevel' => 5, 'scale' => 0.5, 'title' => "$location->listing_id", 'latitude' => $location->latitude, 'longitude' => $location->longitude ];
}
return view('admin.dashboard', compact('locations'));
我将如何删除 foreach 循环中的引号,尤其是 'targetSVG'.
我试过:
json_decode
json_encode
str_replace
条形斜线
这就是我将其附加到 Javascript
中的方式
/* 更新 */
这就是我想要的结果,我想我可以在控制器中插入实际的 SVG 代码:
foreach( $listingLocations as $location ){
$locations[] = [
'svgPath' => 'M9,0C4.029,0,0,4.029,0,9s4.029,9,9,9s9-4.029,9-9S13.971,0,9,0z M9,15.93 c-3.83,0-6.93-3.1-6.93-6.93S5.17,2.07,9,2.07s6.93,3.1,6.93,6.93S12.83,15.93,9,15.93 M12.5,9c0,1.933-1.567,3.5-3.5,3.5S5.5,10.933,5.5,9S7.067,5.5,9,5.5 S12.5,7.067,12.5,9z',
'zoomLevel' => 9,
'scale' => 0.5,
'title' => "$location->listing_id",
'latitude' => $location->latitude,
'longitude' => $location->longitude
];
}
如果整个 'collection' 是一个字符串,则只能删除斜杠。否则,您创建了一个 JSON 对象,如果它是一个字符串,则必须在引号中包含值 - 如本例所示。
遍历 javascript 中的数组并在其中分配变量值:
var targetSVG = // your value
data.forEach(function(item){
item.svgPath = targetSVG
})
我试图在地图上显示一些列表,但我无法在部分中显示 'targetSVG',因为它包含“”(引号)。我告诉你我的意思:
这是它现在在查看源代码部分的显示方式:
如您所见,"targetSVG" 显示引号,我需要将其删除。
这就是我在控制器中设置对象的方式:
$listingLocations = Location::all();
$locations = [];
foreach( $listingLocations as $location ){
$locations[] = ['svgPath' => 'targetSVG', 'zoomLevel' => 5, 'scale' => 0.5, 'title' => "$location->listing_id", 'latitude' => $location->latitude, 'longitude' => $location->longitude ];
}
return view('admin.dashboard', compact('locations'));
我将如何删除 foreach 循环中的引号,尤其是 'targetSVG'.
我试过: json_decode json_encode str_replace 条形斜线
这就是我将其附加到 Javascript
中的方式/* 更新 */
这就是我想要的结果,我想我可以在控制器中插入实际的 SVG 代码:
foreach( $listingLocations as $location ){
$locations[] = [
'svgPath' => 'M9,0C4.029,0,0,4.029,0,9s4.029,9,9,9s9-4.029,9-9S13.971,0,9,0z M9,15.93 c-3.83,0-6.93-3.1-6.93-6.93S5.17,2.07,9,2.07s6.93,3.1,6.93,6.93S12.83,15.93,9,15.93 M12.5,9c0,1.933-1.567,3.5-3.5,3.5S5.5,10.933,5.5,9S7.067,5.5,9,5.5 S12.5,7.067,12.5,9z',
'zoomLevel' => 9,
'scale' => 0.5,
'title' => "$location->listing_id",
'latitude' => $location->latitude,
'longitude' => $location->longitude
];
}
如果整个 'collection' 是一个字符串,则只能删除斜杠。否则,您创建了一个 JSON 对象,如果它是一个字符串,则必须在引号中包含值 - 如本例所示。
遍历 javascript 中的数组并在其中分配变量值:
var targetSVG = // your value
data.forEach(function(item){
item.svgPath = targetSVG
})