Conditions : There is a square matrix filled with numbers from -10 to 10.
Task : Convert the original matrix so that within each column all negative numbers are always below positive numbers vertically, while respecting their original order in relation to each other.
<?php function squareMatrixToString($squareMatrix) { $result = '<hr>'; $squareMatrixSize = count($squareMatrix); for($i = 0; $i < $squareMatrixSize; $i++) { for($j = 0; $j < $squareMatrixSize; $j++) { $result .= $squareMatrix[$i][$j] . ' '; } $result .= '<br>'; } return $result; } /*** Generate squareMatrix ***/ $squareMatrixSize = 10; $squareMatrix = array(); for($i = 0; $i < $squareMatrixSize; $i++) { for($j = 0; $j < $squareMatrixSize; $j++) { $squareMatrix[$i][$j] = 10 - rand(0, 20); } } echo squareMatrixToString($squareMatrix); // TO DO: echo squareMatrixToString($squareMatrix); ?> //на что хватило for($col = 0; $col < 10; $col++) usort($squareMatrix, $sort = function($a, $b) use ($col){ if($a[$col] === $b[$col]) return 0; return ($a[$col] < $b[$col]) ? 1 : -1; }); 