我有这个php数组:
Array
(
[0] => Array
(
[name] => Name1
[schedule] => Array
(
[0] => Array
(
[attractie] => Hey
[start] => 0930
[end] => 1200
)
[1] => Array
(
[attractie] => There
[start] => 1300
[end] => 1400
)
)
[link] => link
[color] => 964d11
)
[1] => Array
(
[name] => Name2
[schedule] => Array
(
[0] => Array
(
[attractie] => Hey
[start] => 1200
[end] => 1530
)
[1] => Array
(
[attractie] => There
[start] => 1000
[end] => 1100
)
)
[link] => link
[color] => e49fca
)
)
我的目标是获得color
值,所以我写了这个循环:
var orders = <?php echo json_encode($myOrders); ?>;
jQuery.each( orders, function(key, value) {
console.log(value.color);
});
现在我期待结果如下:
964d11
e49fca
但结果实际上是这样的:
964d11
e49fca
964d11
e49fca
964d11
e49fca
964d11
e49fca
任何人都知道这里发生了什么? 非常感谢提前!
答案 0 :(得分:0)
这应该按预期工作,您是否尝试使用console.log(this.color);
?
否则,您可以尝试以下方法:
for(var i = 0; i < orders.length; i++){
console.log(orders[i].color);
}
答案 1 :(得分:0)
试试这个
var orders_json = '<?php echo json_encode($myOrders); ?>';
var orders = jQuery.parseJSON(orders_json);
jQuery.each(orders, function (key, value) {
console.log(value['color']);
});
答案 2 :(得分:0)
请尝试以下代码
var eachOrders = <?php echo json_encode($myOrders); ?>;
jQuery.each( eachOrder, function(key, value) {
console.log(value[key][color]);
});
希望这会起作用