简体   繁体   English

PHP-将json_encode数组传递给jQuery会更改格式和类型

[英]PHP - Passing json_encode Array to jQuery changes format and type

PHP Array Output print_R($color_str); PHP数组输出 print_R($color_str);

Array
(
    [0] => '#ff0000','#4caf50','#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'
)

Pass array to jQuery 将数组传递给jQuery

var color = new Array();
color = <?php echo json_encode($color_str) ?>;

console.log(color); outputs below 下面的输出

["'#ff0000','#4caf50','#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'"]

To access the array using key, 要使用键访问阵列,

for (var i = 0; i < 8; i++) {

    var color_str = [color[i]];
    console.log(color_str); //prints  ["'#ff0000','#4caf50','#4caf50','#4caf50','#00bcd4'"]
    console.log(jQuery.type(color_str)); //prints array
}

The double quotes are creating issue and while replacing it, its type changes to string instead of array 双引号正在创建问题,并且在替换问题时,其类型更改为字符串而不是array

var strReplace = color_str[i].replace(/\,/g, "','");
finalColorStr = "'" + strReplace + "'";

The colors param expect an array as input. colors参数期望将数组作为输入。

var options = {
                width: 700,
                height: 350,
                legend: {position: 'none'},
                bar: {groupWidth: '50%'},
                isStacked: true,
                colors: finalColorStr
            };

How do I alter the array value to change its format like below and maintaining the type array ? 如何更改数组值以更改其格式,如下所示并维护array类型?

["#ff0000", "#4caf50", "#4caf50", "#4caf50", "#00bcd4"]
<?php
// that's what you apparently have right now
$x = array(
    "'#ff0000','#4caf50','#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'"
);


$y = array_map( // for each "row" in $x
    function($e) {
        // get everything between ' as one respective element
        preg_match_all("!'([^']+)'!", $e, $m);
        return $m[1];
    },
    $x
);

echo json_encode($y);

that will give you an array of arrays like 这会给你一个像

[
  ["#ff0000", ..., "#00bcd4"],
  ["#00bcd4", ..., "#4caf50"],
  ...
]

But a better option would be not to start with 但是更好的选择不是开始于

array(
  "'#ff0000','#4caf50','#4caf50','#4caf50','#00bcd4'",
  "'#00bcd4','#4caf50','#4caf50','#4caf50','#4caf50'"
  ..
)

anyway. 无论如何。 But I don't know if you have a choice or not .... 但是我不知道你是否有选择...。

Array
(
    [0] => Array('#ff0000','#4caf50','#4caf50','#4caf50','#00bcd4'),
    [1] => Array('#00bcd4','#4caf50','#4caf50','#4caf50','#4caf50'),
    [2] => Array('#4caf50','#4caf50','#4caf50','#4caf50','#4caf50'),
    [3] => Array('#4caf50','#4caf50','#4caf50','#4caf50','#4caf50'),
    [4] => Array('#4caf50','#4caf50','#4caf50','#4caf50','#4caf50'),
    [5] => Array('#4caf50','#4caf50','#4caf50','#4caf50','#4caf50'),
    [6] => Array('#4caf50','#4caf50','#4caf50','#4caf50','#4caf50'),
    [7] => Array('#4caf50','#4caf50','#4caf50','#4caf50','#4caf50')
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM