Home > Backend Development > PHP Tutorial > 输出数组给js用的问题

输出数组给js用的问题

WBOY
Release: 2016-06-23 14:23:22
Original
920 people have browsed it

 data = [{y: 100,color: colors[0],drilldown: {name: 'MSIE versions',categories: ['MSIE 6.0', 'MSIE 7.0', 'MSIE 8.0', 'MSIE 9.0'],data: [10.85, 7.35, 33.06, 2.81],color: colors[0]}}, {y: 21.63,color: colors[1],drilldown: {name: 'Firefox versions',categories: ['Firefox 2.0', 'Firefox 3.0', 'Firefox 3.5', 'Firefox 3.6', 'Firefox 4.0'],data: [0.20, 0.83, 1.58, 13.12, 5.43],color: colors[1]}}, {y: 11.94,color: colors[2],drilldown: {name: 'Chrome versions',categories: ['Chrome 5.0', 'Chrome 6.0', 'Chrome 7.0', 'Chrome 8.0', 'Chrome 9.0','Chrome 10.0', 'Chrome 11.0', 'Chrome 12.0'],data: [0.12, 0.19, 0.12, 0.36, 0.32, 9.91, 0.50, 0.22],color: colors[2]}}, {y: 7.15,color: colors[3],drilldown: {name: 'Safari versions',categories: ['Safari 5.0', 'Safari 4.0', 'Safari Win 5.0', 'Safari 4.1', 'Safari/Maxthon','Safari 3.1', 'Safari 4.1'],data: [4.55, 1.42, 0.23, 0.21, 0.20, 0.19, 0.14],color: colors[3]}}, {y: 2.14,color: colors[4],drilldown: {name: 'Opera versions',categories: ['Opera 9.x', 'Opera 10.x', 'Opera 11.x'],data: [ 0.12, 0.37, 1.65],color: colors[4]}}]; 
Copy after login

以上是javascript原本的数组

data = [{"y":673,"color":"colors[0]","drilldown":{"name":"AMZ_AC \u660e\u7ec6","categories":["DE","ES","FR","GB","IT"],"data":["28","2","410","211","22"],"color":"colors[0]"}},{"y":290,"color":"colors[1]","drilldown":{"name":"AM_AC \u660e\u7ec6","categories":["JP"],"data":["290"],"color":"colors[1]"}},{"y":222,"color":"colors[2]","drilldown":{"name":"CO_AC \u660e\u7ec6","categories":["JP"],"data":["222"],"color":"colors[2]"}}];
Copy after login

以上是我用PHP输出的数组

请问这两个哪里不一样啊??输出产生的结果跟原来的结果差好远


回复讨论(解决方案)

有两个地方显示不了
第一个颜色
"color":"colors[0]" 这里面的数字好像不起作用,全是一种颜色


第二个就是晕些数字
"data":["28","2","410","211","22"] 显示不出来

是不是要转成整型啊,如果要转的话,那颜色里面的数字怎么转呢?
颜色我是这样赋值的
$colors="colors[".$i."]";
'color'=>$colors,

区别大了去了
js 的 color: colors[0], 很明显 colors[0] 是数组 colors 的一个元素
php 的 "color":"colors[0]",这个 colors[0] 只不过是一个串

那怎么改成它那样的呢?

显然你是用 echo json_encode($data); 进行输出的
你需要在输出前去掉 colors[n] 两端的引号
$s = json_encode($data);
echo preg_replace('/"(colors\[\d+\])"/', '$1', $s);

或者直接给 color 赋颜色值

可以了,谢谢啦!

用preg_replace是不是属于比较暴力的方法?呵呵

是的,preg_replace是属于比较暴力的方法
但是你的 colors 是 js 的数组,php 又不可能知道。只能当做字符串处理了

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template