如何使用默认值数组和新值数组生成数组?
How to generate an array using a default values array and a new values array?
我有一个数组$d_visitors = array_count_values($d_visitors);
array:7 [▼
2 => 4
5 => 1
8 => 2
3 => 1
1 => 2
9 => 3
0 => 2
]
我正在尝试遍历该数组 24 次,并检查键 是否匹配 ,并存储它的值。
$dv = [];
for ($i = 0; $i < 24; $i++){
foreach ($d_visitors as $k =>$v) {
if($i == $k ){
$dv[$i] = $v;
}else{
$dv[$i] = 0;
}
}
}
我正在尝试打印出如下内容:
array:24 [▼
0 => 2
1 => 2
2 => 4
3 => 1
4 => 0
5 => 1
6 => 0
7 => 0
8 => 2
9 => 3
10 => 0
11 => 0
12 => 0
13 => 0
14 => 0
15 => 0
16 => 0
17 => 0
18 => 0
19 => 0
20 => 0
21 => 0
22 => 0
23 => 0
]
但我一直收到这个:
array:24 [▼
0 => 2
1 => 0
2 => 0
3 => 0
4 => 0
5 => 0
6 => 0
7 => 0
8 => 0
9 => 0
10 => 0
11 => 0
12 => 0
13 => 0
14 => 0
15 => 0
16 => 0
17 => 0
18 => 0
19 => 0
20 => 0
21 => 0
22 => 0
23 => 0
]
试试这个方法:
$dv = [];
for ($i = 0; $i < 24; $i++){
$dv[$i] = 0;
if (isset($d_visitors[$i])) {
$dv[$i] = $d_visitors[$i];
}
}
更简单的是:
$dv = [];
for ($i = 0; $i < 24; $i++){
$dv[$i] = isset($d_visitors[$i])? $d_visitors[$i] : 0;
}
您的代码中的问题在第 $dv[$i] = 0;
行,因为它设置为零 $dv[$i]
,之前已设置。
您需要使用一个 flag
变量。你的代码也是正确的。
这样试试:
$dv = [];
$flag = 0;
for ($i = 0; $i < 24; $i++){
$flag = 0;
foreach ($d_visitors as $k =>$v) {
if($i == $k ){
$dv[$i] = $v;
$flag = 1;
}
}
if($flag == 0){
$dv[$i] = 0;
}
}
Another way of asking your process is:
How can I:
- merge a default array with another array based on keys
- and
ksort()
the result array?
While you can achieve your desired result using a foreach()
loop and a condition statement on each iteration, I can show you a more concise, simple approach:
This is your new array of data: $array=[2=>4,5=>1,8=>2,3=>1,1=>2,9=>3,0=>2];
There are two ways to set up your default data array:
$defaults=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]; // 24 elements with 0 value
or more elegantly:
$defaults=array_fill(0,24,0);
Then you only need to call array_replace()
to overwrite the default values with the new values. This will keep the keys in ASC order.
var_export(array_replace($defaults,$array));
You can even nest the function calls and avoid adding variable names to the global scope like this:
$d_visitors=array_replace(array_fill(0,24,0),array_count_values($d_visitors)));
Done -- merged and ksorted.
To show some other ways that programmers might try to combine the two arrays (fruitlessly or illogically), here is a demonstration.
我有一个数组$d_visitors = array_count_values($d_visitors);
array:7 [▼
2 => 4
5 => 1
8 => 2
3 => 1
1 => 2
9 => 3
0 => 2
]
我正在尝试遍历该数组 24 次,并检查键 是否匹配 ,并存储它的值。
$dv = [];
for ($i = 0; $i < 24; $i++){
foreach ($d_visitors as $k =>$v) {
if($i == $k ){
$dv[$i] = $v;
}else{
$dv[$i] = 0;
}
}
}
我正在尝试打印出如下内容:
array:24 [▼
0 => 2
1 => 2
2 => 4
3 => 1
4 => 0
5 => 1
6 => 0
7 => 0
8 => 2
9 => 3
10 => 0
11 => 0
12 => 0
13 => 0
14 => 0
15 => 0
16 => 0
17 => 0
18 => 0
19 => 0
20 => 0
21 => 0
22 => 0
23 => 0
]
但我一直收到这个:
array:24 [▼
0 => 2
1 => 0
2 => 0
3 => 0
4 => 0
5 => 0
6 => 0
7 => 0
8 => 0
9 => 0
10 => 0
11 => 0
12 => 0
13 => 0
14 => 0
15 => 0
16 => 0
17 => 0
18 => 0
19 => 0
20 => 0
21 => 0
22 => 0
23 => 0
]
试试这个方法:
$dv = [];
for ($i = 0; $i < 24; $i++){
$dv[$i] = 0;
if (isset($d_visitors[$i])) {
$dv[$i] = $d_visitors[$i];
}
}
更简单的是:
$dv = [];
for ($i = 0; $i < 24; $i++){
$dv[$i] = isset($d_visitors[$i])? $d_visitors[$i] : 0;
}
您的代码中的问题在第 $dv[$i] = 0;
行,因为它设置为零 $dv[$i]
,之前已设置。
您需要使用一个 flag
变量。你的代码也是正确的。
这样试试:
$dv = [];
$flag = 0;
for ($i = 0; $i < 24; $i++){
$flag = 0;
foreach ($d_visitors as $k =>$v) {
if($i == $k ){
$dv[$i] = $v;
$flag = 1;
}
}
if($flag == 0){
$dv[$i] = 0;
}
}
Another way of asking your process is:
How can I:
- merge a default array with another array based on keys
- and
ksort()
the result array?
While you can achieve your desired result using a foreach()
loop and a condition statement on each iteration, I can show you a more concise, simple approach:
This is your new array of data: $array=[2=>4,5=>1,8=>2,3=>1,1=>2,9=>3,0=>2];
There are two ways to set up your default data array:
$defaults=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]; // 24 elements with 0 value
or more elegantly:
$defaults=array_fill(0,24,0);
Then you only need to call array_replace()
to overwrite the default values with the new values. This will keep the keys in ASC order.
var_export(array_replace($defaults,$array));
You can even nest the function calls and avoid adding variable names to the global scope like this:
$d_visitors=array_replace(array_fill(0,24,0),array_count_values($d_visitors)));
Done -- merged and ksorted.
To show some other ways that programmers might try to combine the two arrays (fruitlessly or illogically), here is a demonstration.