从javascript中的Loop语句中获取数组值

时间:2016-08-10 06:14:54

标签: javascript php arrays loops

我的PHP代码就是这个

$newArr = array();

$newArr['Item1']=$array1;
$newArr['Item2']=$array2;
$newArr['Item3']=$array3;
$newArr['Item4']=$array4;
$newArr['Item5']=$array5;
$newArr['Item6']=$array6;
$newArr['Item8']=$array8;

echo json_encode($newArr);

我想用chartjs构建包含这些数据的折线图。所以图表看起来像这样......

enter image description here

这是我目前的代码js ......

$(document).ready(function(){
$.ajax({
    url : "http://localhost/clicklog/clicklog.php",
    type : "GET",
    success : function(newArr){
        //console.log(newArr.Item1);


        Clicks1 = [];
        Clicks2 = [];
        Clicks3 = [];
        Clicks4 = [];
        Clicks5 = [];
        Clicks6 = [];
        Clicks8 = [];

        for(var i in newArr){

            //console.log(newArr[i]);

            for(var x in newArr[i]){
                //console.log(newArr[i][x]);
                Clicks1.push(newArr[i][x]);

            }

        }

        console.log(Clicks1);

使用此代码,我的变量“Clicks1”具有此值

[18948, 13783, 9484, 5468, 46267, 33511, 22824, 13204, 5378, 3773, 2564, 1586, 18076, 13252, 8979, 5394, 10273, 7139, 4713, 2632, 8812, 6326, 4465, 2751, 18083, 12924, 8886, 5354]

来自“newArray”而没有排序。

我想仅为那些来自“第1项”的数据赋予“var clicks1”值

这是我的PHP代码的结果..

{"Item1":{"2009-04-17":18948,"2009-04-18":13783,"2009-04-19":9484,"2009-04-20":5468},
"Item2":{"2009-04-17":46267,"2009-04-18":33511,"2009-04-19":22824,"2009-04-20":13204},
"Item3":{"2009-04-17":5378,"2009-04-18":3773,"2009-04-19":2564,"2009-04-20":1586},
"Item4":{"2009-04-17":18076,"2009-04-18":13252,"2009-04-19":8979,"2009-04-20":5394},
"Item5":{"2009-04-17":10273,"2009-04-18":7139,"2009-04-19":4713,"2009-04-20":2632},
"Item6":{"2009-04-17":8812,"2009-04-18":6326,"2009-04-19":4465,"2009-04-20":2751},
"Item8":{"2009-04-17":18083,"2009-04-18":12924,"2009-04-19":8886,"2009-04-20":5354}}

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

  

我想仅为那些来自“第1项”的数据赋予“var clicks1”值

这是你如何做到的,但请继续阅读:

var newArr = {"Item1":{"2009-04-17":18948,"2009-04-18":13783,"2009-04-19":9484,"2009-04-20":5468},
"Item2":{"2009-04-17":46267,"2009-04-18":33511,"2009-04-19":22824,"2009-04-20":13204},
"Item3":{"2009-04-17":5378,"2009-04-18":3773,"2009-04-19":2564,"2009-04-20":1586},
"Item4":{"2009-04-17":18076,"2009-04-18":13252,"2009-04-19":8979,"2009-04-20":5394},
"Item5":{"2009-04-17":10273,"2009-04-18":7139,"2009-04-19":4713,"2009-04-20":2632},
"Item6":{"2009-04-17":8812,"2009-04-18":6326,"2009-04-19":4465,"2009-04-20":2751},
"Item8":{"2009-04-17":18083,"2009-04-18":12924,"2009-04-19":8886,"2009-04-20":5354}};

var Clicks1 = [];
var data1 = newArr.Item1;
for (x in data1) {
   Clicks1.push(data1[x]);
}
console.log(Clicks1);

...但是你必须继续为Clicks2Clicks7继续这样做。无论何时,在任何编程语言中,使用“var1,var2,var3,var4”等变量,它都会告诉您要使用数组

如果没有更多上下文,我就不能具体说明如何更改它,因为我们不知道您将Clicks1通过Clicks7做什么,但您很可能会想要更改PHP以处理数组(非关联类),然后在JavaScript代码中使用结果数组。

但是对于它的价值,这将为您提供一个单独的数组,其中包含现有响应中每个ItemX的数据,按ItemX标签排序,名为allclicks的数组:

var newArr = {"Item1":{"2009-04-17":18948,"2009-04-18":13783,"2009-04-19":9484,"2009-04-20":5468},
"Item2":{"2009-04-17":46267,"2009-04-18":33511,"2009-04-19":22824,"2009-04-20":13204},
"Item3":{"2009-04-17":5378,"2009-04-18":3773,"2009-04-19":2564,"2009-04-20":1586},
"Item4":{"2009-04-17":18076,"2009-04-18":13252,"2009-04-19":8979,"2009-04-20":5394},
"Item5":{"2009-04-17":10273,"2009-04-18":7139,"2009-04-19":4713,"2009-04-20":2632},
"Item6":{"2009-04-17":8812,"2009-04-18":6326,"2009-04-19":4465,"2009-04-20":2751},
"Item8":{"2009-04-17":18083,"2009-04-18":12924,"2009-04-19":8886,"2009-04-20":5354}};

// An array we'll put arrays inside
var allclicks = [];
// Loop through the items in alphabetical order
Object.keys(newArr).sort().forEach(function(itemKey) {
  // Get the item for this key
  var item = newArr[itemKey];
  
  // Add an array to allclicks for this item's clicks
  itemclicks = allclicks[allclicks.length] = [];
  
  // Loop through this items dates to get the clicks
  for (date in item) {
    itemclicks.push(item[date]);
  }
});
console.log(allclicks);

然后您使用allclicks[0]allclicks[1]等,而不是Clicks1Clicks2等。