如何计算每列的数组值?

我有这样的数组


$chunk  = 4; 


$arr    = array('1', '1', '1', '1', '1', '1', '1', '0', '1', '0', '1', '0', '0', '0', '0', '1');

我根据将该数组修改为行和列$chunk。在此示例中,它将是4x4的行和列。


我想做的是如何计算每列中有多少个“ 1”或“ 0”,并打印出“ 1”或“ 0”中最大的输出。


例子:


1110

1101

1110

1001


there are 4 number '1' in first column, so the output should be 4

我当前的代码:


function array_chunk_vertical($arr, $percolnum){

$n = count($arr);

$mod    = $n % $percolnum;

$cols   = floor($n / $percolnum);

$mod ? $cols++ : null ;

$re     = array();

for($col = 0; $col < $cols; $col++){

    for($row = 0; $row < $percolnum; $row++){

        if($arr){

            $re[$row][]   = array_shift($arr);

        }

    }

}

return $re;

}


$result = array_chunk_vertical($arr, $chunk);

$new    = 0;

$exist  = 0;


foreach($result  as $row){

    foreach($row as $val){

        if($val == "1"){

            $new++;

        }

        else{

            $exist++;

        }

            echo $val;          

    }

    echo '<br/>';

}


慕尼黑5688855
浏览 172回答 3
3回答

墨色风雨

$chunk&nbsp; = 4;&nbsp;$arr&nbsp; &nbsp; = array('1', '1', '1', '1', '1', '1', '1', '0', '1', '0', '1', '0', '0', '0', '0', '1');// Split your array in chunks$chunks = array_chunk($arr, 4);// Max counts for 1 and 0&nbsp;$max1 = $max0 = 0;foreach ($chunks as $chunk) {&nbsp; &nbsp; // count number of occurences of 1 and 0&nbsp; &nbsp; $countedValues = array_count_values($chunk);&nbsp; &nbsp; $cur1 = isset($countedValues[1]) ? $countedValues[1] : 0;&nbsp; &nbsp; $cur0 = isset($countedValues[0]) ? $countedValues[0] : 0;&nbsp; &nbsp; echo 'count 1 is ' . $cur1 . PHP_EOL;&nbsp; &nbsp; echo 'count 0 is ' . $cur0 . PHP_EOL;&nbsp; &nbsp; $max1 = max($max1, $cur1);&nbsp; &nbsp; $max0 = max($max0, $cur0);}echo '1 max count is ' . $max1 . PHP_EOL . '0 max count is ' . $max0;

慕村225694

简短的代码段可帮助您获得结果。$i = 0;foreach($result as $k => $v){&nbsp; &nbsp; $temp[$k] = count(array_filter(array_column($result,$i)));&nbsp; &nbsp; $i++;}在上面的代码中,array_column将从索引为0,索引为1等的所有数组中获取数据。array_filter将删除0个值(默认性质)。下面的输出包括所有垂直第1个的计数。输出Array(&nbsp; &nbsp; [0] => 4&nbsp; &nbsp; [1] => 3&nbsp; &nbsp; [2] => 2&nbsp; &nbsp; [3] => 1)

牛魔王的故事

您可以按如下所示对列1进行0计数并通过修改最后一个foreach循环进行计数$columns = array();&nbsp;foreach($result&nbsp; as $row){&nbsp; &nbsp;foreach($row as $key => $val){&nbsp; &nbsp; $columns[$key][] = $val;&nbsp; &nbsp; if($val == "1"){&nbsp; &nbsp; &nbsp; &nbsp; $new++;&nbsp; &nbsp; }&nbsp; &nbsp; else{&nbsp; &nbsp; &nbsp; &nbsp; $exist++;&nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; echo $val;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}&nbsp; echo '<br/>';&nbsp;}$cols = array_map(function($v){&nbsp; &nbsp;$temp = array_count_values($v);&nbsp; &nbsp;return 'Element :'.array_keys($temp, max($temp))[0].' came '.max($temp).' times.';&nbsp;}, $columns);echo '<pre>';print_r($cols);结果Array(&nbsp;[0] => Element :1 came 4 times.&nbsp;[1] => Element :1 came 3 times.&nbsp;[2] => Element :1 came 2 times.&nbsp;[3] => Element :0 came 3 times.)
打开App,查看更多内容
随时随地看视频慕课网APP