在此示例中,如何计算每行中有多少 NULL 值、多少个零 (0) 和多少个 (1) 以及每个值存储在变量中。+-------+--------+--------+--------+--------+--------+--------+--------+--------+--------+| StdID | day1S1 | day1S2 | day1S3 | day2S1 | day2S2 | day2S3 | day3S1 | day3S2 | day3S3 |+-------+--------+--------+--------+--------+--------+--------+--------+--------+--------+| 3 | NULL | 1 | 0 | 1 | 1 | 0 | NULL | 1 | 1 || 4 | 1 | 1 | 0 | 1 | 0 | 0 | NULL | 1 | 1 |+-------+--------+--------+--------+--------+--------+--------+--------+--------+--------+开/关**StdID = 3**$null = 2;$zeros = 2;$ones = 5;**StdID = 4**$null = 1;$zeros = 3;$ones = 5;我不知道该怎么做,但我正在尝试使用以下代码$check = isset($day1['day1S1']) ? "1" : "0"; doesnt work$values = $day1['day1S1']+$day1['day1S2']+$day1['day1S3'] etc请帮我解决这个问题。谢谢
1 回答
慕盖茨4494581
TA贡献1850条经验 获得超11个赞
array_walk - 将用户提供的函数应用于数组的每个成员
array_count_values - 计算数组的所有值
例如array_walk
,您可以使用,array_count_values
函数
$arr = [
'StdID' => [
'3' => ['NULL',1,0,1,1,0,'NULL',1,1],
'4' => [1,1,0,1,0,0,'NULL',1,1]
]
];
$res=[];
array_walk($arr['StdID'], function($v, $k) use (&$res){
$res['StdID'][$k] = array_count_values($v);
});
echo '<pre>';
print_r($res);
输出
Array
(
[StdID] => Array
(
[3] => Array
(
[NULL] => 2
[1] => 5
[0] => 2
)
[4] => Array
(
[1] => 5
[0] => 3
[NULL] => 1
)
)
)
- 1 回答
- 0 关注
- 147 浏览
添加回答
举报
0/150
提交
取消