我在这个数组上遇到错误。数组是:
$array = array(
['parkingMode'] => array(
[0] => 123
[1] => 124
[2] => 266
),
['owlMode'] => array(
[0] => 124
[1] => 129
[2] => 266
)
);
这怎么可能是正确的?
更新:
现在我提出我的问题。假设我有一个像
这样的空数组`$newArray = array("parkingMode"=>array(),
"owlMode"=>array()
);
我正在做的是将元素推入foreach循环中,如
foreach($devices as $k => $v) {
$dev = $redis->hGetAll($v);
//print_r($dev);
if(array_key_exists('owlMode', $dev)){
if($dev['owlMode'] =='on'){
array_push($newArray["parkingMode"], $k);
}
}
if(array_key_exists('parkingMode', $dev)){
if($dev['parkingMode'] =='on'){
array_push($newArray["owlMode"], $k);
}
}
}
//print_r($newArray);
那么为什么这会给我一个类似数组的结果:
$array = array(
['parkingMode'] => array(
[0] => 123
[1] => 124
[2] => 266
),
['owlMode'] => array(
[0] => 124
[1] => 129
[2] => 266
)
);
`
答案 0 :(得分:5)
数组中的元素应以逗号,
分隔。这是正确的格式:
$array = array(
'parkingMode' => array(
0 => 123,
1 => 124,
2 => 266
),
'owlMode' => array(
0 => 124,
1 => 129,
2 => 266
)
);
答案 1 :(得分:1)
您应该创建关联数组。在关联数组中,分配给值的键可以是任意的和用户定义的字符串。
array(
"parkingMode" => array(
[0] => 123,
[1] => 124,
[2] => 266
), "owlMode" => array(
[0] => 124,
[1] => 129,
[2] => 266
));