PHP用列值计算每一行并存储在一个变量中

在此示例中,如何计算每行中有多少 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

请帮我解决这个问题。谢谢


幕布斯6054654
浏览 148回答 1
1回答

慕盖茨4494581

array_walk&nbsp;- 将用户提供的函数应用于数组的每个成员array_count_values&nbsp;- 计算数组的所有值例如array_walk,您可以使用,array_count_values函数$arr = [&nbsp; 'StdID' => [&nbsp; &nbsp; '3' => ['NULL',1,0,1,1,0,'NULL',1,1],&nbsp; &nbsp; '4' => [1,1,0,1,0,0,'NULL',1,1]&nbsp; ]];$res=[];array_walk($arr['StdID'], function($v, $k) use (&$res){&nbsp; &nbsp;$res['StdID'][$k] = array_count_values($v);});echo '<pre>';print_r($res);输出Array(&nbsp; [StdID] => Array&nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; [3] => Array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [NULL] => 2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [1] => 5&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [0] => 2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; [4] => Array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [1] => 5&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [0] => 3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [NULL] => 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; ))
打开App,查看更多内容
随时随地看视频慕课网APP