计算二维数组

时间:2018-09-09 16:37:24

标签: php multidimensional-array

我的二维数组如下:

$arrRes = array(  
                array("2018-08-01"=>"X" , "2018-08-02"=>"O" , "2018-08-03"=>"O" , "2018-08-04"=>"O"),
                array("2018-08-01"=>"X" , "2018-08-02"=>"X" , "2018-08-03"=>"O" , "2018-08-04"=>"O"),
                array("2018-08-01"=>"X" , "2018-08-02"=>"X" , "2018-08-03"=>"O" , "2018-08-04"=>"X"),
                array("2018-08-01"=>"X" , "2018-08-02"=>"O" , "2018-08-03"=>"O" , "2018-08-04"=>"O"),
           );  

如何计算行和列数组中的“ X”和“ O”? 我想要结果:

//数字行“ X”:

Array
(
[0] => 1
[1] => 2
[2] => 2
[3] => 1
)

//数字行“ O”:

Array
(
[0] => 3
[1] => 2
[2] => 2
[3] => 3
)

// NUMBER列“ X”

Array
(
[0] => 4
[1] => 2
[2] => 0
[3] => 1
)

// NUMBER列“ O”

Array
(
[0] => 0
[1] => 2
[2] => 4
[3] => 3
)

我已经尝试过,但是我很困惑:

foreach($arrRes as $keyRow => $row){
       foreach($row as $keyCol => $col){
            if($col == 'X'){
            ????     
             }elseif($col == 'O'){
            ????     
             }
      } 
    } 

3 个答案:

答案 0 :(得分:1)

以splash58回答为起点,我添加了各个部分以希望使其完整。

我使用array_merge(['X' => 0, 'O' => 0], array_count_values($row));,它将始终确保存在“ X”和“ O”条目,这在列中显示的多于行。

对于列,我使用array_column()提取每一列并以与行相同的方式对其进行处理。

$arrRes = array(
    array("2018-08-01"=>"X" , "2018-08-02"=>"O" , "2018-08-03"=>"O" , "2018-08-04"=>"O"),
    array("2018-08-01"=>"X" , "2018-08-02"=>"X" , "2018-08-03"=>"O" , "2018-08-04"=>"O"),
    array("2018-08-01"=>"X" , "2018-08-02"=>"X" , "2018-08-03"=>"O" , "2018-08-04"=>"X"),
    array("2018-08-01"=>"X" , "2018-08-02"=>"O" , "2018-08-03"=>"O" , "2018-08-04"=>"O"),
);  
$row =[];
$col = [];
foreach($arrRes as $row){
    $res[] = array_merge(['X' => 0, 'O' => 0], array_count_values($row));
}
$rowX = array_column($res,"X");
$rowO = array_column($res,"O");
foreach($arrRes[0] as $key => $row){
    $col[] = array_merge(['X' => 0, 'O' => 0], 
            array_count_values(array_column($arrRes,$key)));
}
$colX = array_column($col,"X");
$colO = array_column($col,"O");

print_r($rowX);
print_r($rowO);
print_r($colX);
print_r($colO);

这给出了输出...

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 1
)
Array
(
    [0] => 3
    [1] => 2
    [2] => 1
    [3] => 3
)
Array
(
    [0] => 4
    [1] => 2
    [2] => 0
    [3] => 1
)
Array
(
    [0] => 0
    [1] => 2
    [2] => 4
    [3] => 3
)

答案 1 :(得分:1)

已解析/未弄乱的代码段可帮助您实现输出

$printers = array(
    array("2018-08-01" => "X", "2018-08-02" => "O", "2018-08-03" => "O", "2018-08-04" => "O"),
    array("2018-08-01" => "X", "2018-08-02" => "X", "2018-08-03" => "O", "2018-08-04" => "O"),
    array("2018-08-01" => "X", "2018-08-02" => "X", "2018-08-03" => "O", "2018-08-04" => "X"),
    array("2018-08-01" => "X", "2018-08-02" => "O", "2018-08-03" => "O", "2018-08-04" => "O"),
);
//PrinterFilter class to filter our iterator and ONLY return values that start with Printer_
class PrinterFilter extends FilterIterator
{
    public function accept()
    {
        //Only accept values that start with Printer_
        if (false !== strpos(parent::current(), 'X') || false !== strpos(parent::current(), 'O')) {
            return true;
        }
        return false;
    }
}
//Create a new Iterator for the array
//Create a RecursiveArrayIterator around our array
//Create a RecursiveIteratorIterator around that so we can easily navigate it
//Create a PrintFilter interator around that so we can filter
$iterator = new PrinterFilter(new RecursiveIteratorIterator(new RecursiveArrayIterator($printers)));
$counts   = array();
$i        = 0;
$j        = 0;
//Simply iterate over the iterator add new keys as necessary
//Increment existing keys.
foreach ($iterator as $key => $value) {
    $result['rows'][$value][$i] = (isset($counts[$value]) ? ++$counts[$value] : ($counts[$value] = 1));
    $result['cols'][$value][$j] = (isset($result['cols'][$value][$j]) ? ++$result['cols'][$value][$j] : 1);
    $j++;
    if ($j % 4 == 0) {
        $i++;
        $j      = 0;
        $counts = [];
    }
}
foreach ($result['cols'] as $key => $value) {
    $newKeys = array_fill_keys(range(0, 3), 0);
    $result['cols'][$key] += $newKeys;
    ksort($result['cols'][$key]);
}
pr($result);

这里在工作code

答案 2 :(得分:0)

如果您的值已知(X / O),则可以预先创建结果数组。然后循环遍历现有数组,并根据值和行/列位置增加row / col计数。使用array_search ( .. array_keys () )获取关联密钥的密钥索引。

$arraySum = [
             'rows' => ['X'=>[0,0,0,0],'O'=>[0,0,0,0]],
             'cols' => ['X'=>[0,0,0,0],'O'=>[0,0,0,0]]
            ];

foreach($arrRes as $keyRow => $row){
   foreach($row as $keyCol => $col){
      $arraySum['rows'][$col][$keyRow]++;
      $arraySum['cols'][$col][array_search($keyCol,array_keys($row))]++; 
  } 
}

演示-https://3v4l.org/ROTMP