使用此数组值可将其更改为JSON格式。
$prices = array("250", "350", "400", "678", "800", "1000");
var opt = {
milestones: {
1: {
mlPos: 250, ---> (set $price value)
mlId: false,
mlClass: 'bi-custom',
mlDim: '200%',
mlLabel: 'Milestone one',
mlLabelVis: 'hover',
mlHoverRange: 15,
mlLineWidth: 1
},
2: {
mlPos: 350, ---> (set $price value)
mlId: false,
mlClass: 'bi-custom',
mlDim: '200%',
mlLabel: 'Milestone two',
mlLabelVis: 'hover',
mlHoverRange: 15,
mlLineWidth: 1
},
3: {
mlPos: 400, ---> (set $price value)
mlId: false,
mlClass: 'bi-custom',
mlDim: '200%',
mlLabel: 'Milestone one',
mlLabelVis: 'hover',
mlHoverRange: 15,
mlLineWidth: 1
},
4: {
mlPos: 678,---> (set $price value)
mlId: false,
mlClass: 'bi-custom',
mlDim: '200%',
mlLabel: 'Milestone two',
mlLabelVis: 'hover',
mlHoverRange: 15,
mlLineWidth: 1
},
5: {
mlPos: 800,---> (set $price value)
mlId: false,
mlClass: 'bi-custom',
mlDim: '200%',
mlLabel: 'Milestone two',
mlLabelVis: 'hover',
mlHoverRange: 15,
mlLineWidth: 1
}
}
};
我们有数组格式$price
的php变量以这种json格式的javascript变量转换,问题不在于将php变量转换为javascript变量的问题,只是将php数组转换为上述格式的json格式。
任何人请帮忙
谢谢。
javascript参考方案
在使用json_encode将数组转换为对象之前,还可以使用JSON_FORCE_OBJECT
选项:
$prices = array( "250", "350", "400", "678", "800", "1000" );
$row = [
'mlPos' => null,
'mlId' => false,
'mlClass' => 'bi-custom',
'mlDim' => '200%',
'mlLabel' => 'Milestone two',
'mlLabelVis' => 'hover',
'mlHoverRange' => 15,
'mlLineWidth' => 1
];
$rows = [];
foreach( $prices as $price ) {
$rows[] = (object) array_replace( $row, [ 'mlPos' => $price ] );
}
$opt = [ 'milestones' => (object) $rows ];
echo json_encode( $opt, JSON_FORCE_OBJECT );
// output {"milestones":{"0":{"mlPos":"250","mlId":false,"mlClass":"bi-custom", ...
打印二维阵列 - javascript我正在尝试打印子元素。在this example之后。怎么做?。$myarray = array("DO"=>array('IDEAS','BRANDS','CREATIVE','CAMPAIGNS'), "JOCKEY"=>a…
粗糙的Unicode->没有CLDR的语言代码? - javascript我在写字典应用。如果用户键入Unicode字符,我想检查该字符是哪种语言。例如字 - returns ['zh', 'ja', 'ko'] العربية - returns ['ar'] a - returns ['en', 'fr', …
如何在没有for循环的情况下在Javascript中使用Django模板标签 - javascript我想在JavaScript中使用模板变量:我的问题是在javascript代码中使用for循环,for循环之间的所有事情都会重复..但我不想要....下面粘贴了我的代码..有人可以告诉我更好的方法吗这..因为这看起来很丑..这是我的代码: {% block extra_javascript %} <script src="/static/js…
使用JS和PHP更改弹出窗口背景图像 - javascript我有一个JS函数:function zoom(now) { document.getElementById("popup").style.display = "block"; document.getElementById("photos").style.backgroundImage = …
执行onclick时获得意外令牌 - javascript我正在使用onclick事件从PHP调用JS函数。这是我的代码:我在一个函数中,因此我需要通过PHP来完成它,因为然后我会返回:$html = '<input type="checkbox" checked value="1" id="setGetSku" name="se…