Problems with the output array for JS

Source: Internet
Author: User
 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 ', Categori  es: [' 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 ', Cate Gories: [' Opera 9.x ', ' Opera10.x ', ' Opera 11.x '],data: [0.12, 0.37, 1.65],color:colors[4]}];  

The above is the original JavaScript array

data = [{"Y": 673, "Color": "Colors[0]", "drilldown": {"name": "Amz_ac \u660e\u7ec6", "categories": ["DE", "ES", "FR", "GB", "IT"], "Data": ["["], "2", "410", "211", "X", "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] "}}];

The above is the array I output with PHP

Excuse me, where are the two different?? The output produces results that are far worse than the original results.


Reply to discussion (solution)

There's two places I can't show.
First color
"Color": "Colors[0" The numbers don't seem to work, they're all a color.


The second one is to stun some numbers.
"Data": ["28", "2", "410", "211", "22"] does not show

is not to turn into an integral type Ah, if you want to turn, the color inside the number how to turn it?
The color is how I assign it.
$colors = "colors[". $i. "]";
' Color ' = $colors,

It's a big difference.
JS Color:colors[0], obviously colors[0] is an element of an array colors
PHP "Color": "Colors[0]", this colors[0] is just a string

How does it change to that?

Obviously you are using echo json_encode ($data); For the output
You need to remove the quotes at the ends of the colors[n] before the output
$s = Json_encode ($data);
echo preg_replace ('/' (colors\[\d+\]) "/', ' $ ', $s);

or assign a color value directly to color

Yes, thank you!

Does using preg_replace belong to the method of comparing violence? Oh

Yes, Preg_replace is a more violent method.
But your colors is an array of JS, and PHP is impossible to know. Can only be treated as a string

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.