【发布时间】:2015-10-05 12:02:55
【问题描述】:
PHP 数组输出 $color_str
Array
(
[0] => '#4caf50','#00bcd4','#4caf50','#4caf50','#00bcd4'
[1] => '#00bcd4','#4caf50','#4caf50','#4caf50','#4caf50'
[2] => '#4caf50','#4caf50','#4caf50','#4caf50','#4caf50'
[3] => '#4caf50','#4caf50','#4caf50','#4caf50','#4caf50'
[4] => '#4caf50','#4caf50','#4caf50','#4caf50','#4caf50'
[5] => '#4caf50','#4caf50','#4caf50','#4caf50','#4caf50'
[6] => '#4caf50','#4caf50','#4caf50','#4caf50','#4caf50'
[7] => '#4caf50','#4caf50','#4caf50','#4caf50','#4caf50'
)
JSON 编码
["'#4caf50','#00bcd4','#4caf50','#4caf50','#00bcd4'","'#00bcd4','#4caf50','#4caf50','#4caf50','#4caf50'","'#4caf50','#4caf50','#4caf50','#4caf50','#4caf50'","'#4caf50','#4caf50','#4caf50','#4caf50','#4caf50'","'#4caf50','#4caf50','#4caf50','#4caf50','#4caf50'","'#4caf50','#4caf50','#4caf50','#4caf50','#4caf50'","'#4caf50','#4caf50','#4caf50','#4caf50','#4caf50'","'#4caf50','#4caf50','#4caf50','#4caf50','#4caf50'"]
$color_string = json_encode($color_str);
脚本
var options = {
width: 1000,
height: 300,
legend: {position: 'none'},
bar: {groupWidth: '50%'},
isStacked: true,
hAxis: {title: "Resource(s)"},
vAxis: {title: "Week(s)"},
colors: resColorsStr(),
};
function resColorsStr(){
var color_str = [];
for(c = 0; c <= 7 ; c++){
color_str = [<?php echo $color_string [c] ?>];
//color_str.push(<?php echo $color_string [c]?>);
}
console.log(color_str);
return color_str;
}
如何循环并将数组值存储到color_str,所以对于第一个循环color_str = '#4caf50','#00bcd4','#4caf50','#4caf50','#00bcd4',对于第二个
color_str = '#00bcd4','#4caf50','#4caf50','#4caf50','#4caf50'
因此,当函数resColorsStr被调用时,同样会显示颜色。
【问题讨论】:
-
您是否将整个 php 数组存储在任何地方的 js 变量中?由于 php 是服务器端,您可能需要将整个 for 循环放在 php 中,而不仅仅是单个 echo。
-
只需
json_encode(array([0] => '#4caf50',...)并在客户端解析它 -
@NullPoiиteя 请查看我更新的问题。
-
因为你有字符串而不是数组,
"'#4caf50','...,'#00bcd4'"你能看到这里的双引号吗?看到这个codepad.viper-7.com/Ia9zS3 -
@NullPoiиteя ,作为一个新手,我现在应该做什么?能否请您详细说明
标签: javascript php jquery arrays google-visualization