Reputation: 57
i've array 2D as follows:
$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"),
);
How to count "X" and "O" in row and column array? i want the result:
//NUMBER ROW "X":
Array
(
[0] => 1
[1] => 2
[2] => 2
[3] => 1
)
//NUMBER ROW "O":
Array
(
[0] => 3
[1] => 2
[2] => 2
[3] => 3
)
//NUMBER COLUMN "X"
Array
(
[0] => 4
[1] => 2
[2] => 0
[3] => 1
)
//NUMBER COLUMN "O"
Array
(
[0] => 0
[1] => 2
[2] => 4
[3] => 3
)
i've tried but i'm confused:
foreach($arrRes as $keyRow => $row){
foreach($row as $keyCol => $col){
if($col == 'X'){
????
}elseif($col == 'O'){
????
}
}
}
Upvotes: 0
Views: 53
Reputation: 18567
Resolved/untangled Snippet to help you achieve your output
$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);
Here is working code.
Upvotes: 1
Reputation: 57131
Using splash58 answer as a start point, I've added various parts to hopefully make it complete.
I use array_merge(['X' => 0, 'O' => 0], array_count_values($row));
, which will always ensure that a 'X' and a 'O' entries exist, this is shown up more in the columns than the rows.
For the columns, I use array_column()
to extract each column and the process it in the same way as the rows.
$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);
This gives as output...
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
)
Upvotes: 1
Reputation: 12433
If your values are known (X
/O
), you can pre-create your result array. Then loop through your existing array and increase the row
/col
count based on the value and row/column position. Use array_search ( .. array_keys () )
to get the key index of the associative key.
$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))]++;
}
}
demo - https://3v4l.org/ROTMP
Upvotes: 0