[英]How to count array value per column?
我有這樣的數組
$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/>';
}
非常簡短的片段,以幫助您獲得結果。
$i = 0;
foreach($result as $k => $v){
$temp[$k] = count(array_filter(array_column($result,$i)));
$i++;
}
在上面的代碼中, array_column將從具有第0個索引,第1個索引的所有數組中獲取數據,依此類推。
array_filter將刪除0個值(默認屬性)。
下面的輸出包括所有第1個垂直的計數。
產量
Array
(
[0] => 4
[1] => 3
[2] => 2
[3] => 1
)
您可以通過修改上一個foreach循環來計算第1
列和第0
列的計數,如下所示
$columns = array();
foreach($result as $row){
foreach($row as $key => $val){
$columns[$key][] = $val;
if($val == "1"){
$new++;
}
else{
$exist++;
}
echo $val;
}
echo '<br/>';
}
$cols = array_map(function($v){
$temp = array_count_values($v);
return 'Element :'.array_keys($temp, max($temp))[0].' came '.max($temp).' times.';
}, $columns);
echo '<pre>';
print_r($cols);
結果
Array
(
[0] => Element :1 came 4 times.
[1] => Element :1 came 3 times.
[2] => Element :1 came 2 times.
[3] => Element :0 came 3 times.
)
我已經更新了塊方法以簡化它,然后從這個結果我使用array_column()
和array_sum()
來為每列添加總計。 然后你可以使用max()
來找到最高的(注釋也在代碼中)......
$chunk = 4;
$arr = array('1', '1', '1', '1', '1', '1', '1', '0', '1', '0', '1', '0', '0', '0', '0', '1');
function array_chunk_vertical($arr, $percolnum){
$re = [];
foreach ( $arr as $number => $value ) {
// Use modulus % to put value in correct column
$re [$number % $percolnum][] = $value;
}
return $re;
}
$result = array_chunk_vertical($arr, $chunk);
$count = [];
// Loop over first row of output
foreach($result[0] as $column => $row){
// Sum up the results of taking all the column values for this column
$count[$column] = array_sum(array_column($result, $column));
}
// Extract highest
$highest = max($count);
// Find highest and output which one it is.
echo $highest ." in ". (array_search($highest, $count)+1);
結果是
4 in 1
$chunk = 4;
$arr = 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
$max1 = $max0 = 0;
foreach ($chunks as $chunk) {
// count number of occurences of 1 and 0
$countedValues = array_count_values($chunk);
$cur1 = isset($countedValues[1]) ? $countedValues[1] : 0;
$cur0 = isset($countedValues[0]) ? $countedValues[0] : 0;
echo 'count 1 is ' . $cur1 . PHP_EOL;
echo 'count 0 is ' . $cur0 . PHP_EOL;
$max1 = max($max1, $cur1);
$max0 = max($max0, $cur0);
}
echo '1 max count is ' . $max1 . PHP_EOL . '0 max count is ' . $max0;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.