text: Check horizontal and vertical symmetry in binary matrix | PHP program to find if a matrix is symmetric . ; Initializing as both horizontal and vertical symmetric . ; Checking for Horizontal Symmetry . We compare first row with last row , second row with second last row and so on . ; Checking each cell of a column . ; check if every cell is identical ; Checking for Vertical Symmetry . We compare first column with last column , second xolumn with second last column and so on . ; Checking each cell of a row . ; check if every cell is identical ; Driver Code
code: < ? php function checkHV ( $ arr , $ N , $ M ) { $ horizontal = true ; $ vertical = true ; for ( $ i = 0 , $ k = $ N - 1 ; $ i < $ N / 2 ; $ i ++ , $ k -- ) { for ( $ j = 0 ; $ j < $ M ; $ j ++ ) { if ( $ arr [ $ i ] [ $ j ] != $ arr [ $ k ] [ $ j ] ) { $ horizontal = false ; break ; } } } for ( $ i = 0 , $ k = $ M - 1 ; $ i < $ M / 2 ; $ i ++ , $ k -- ) { for ( $ j = 0 ; $ j < $ N ; $ j ++ ) { if ( $ arr [ $ i ] [ $ j ] != $ arr [ $ k ] [ $ j ] ) { $ horizontal = false ; break ; } } } if ( ! $ horizontal && ! $ vertical ) echo " NO STRNEWLINE " ; else if ( $ horizontal && ! $ vertical ) cout << " HORIZONTAL STRNEWLINE " ; else if ( $ vertical && ! $ horizontal ) echo " VERTICAL STRNEWLINE " ; else echo " BOTH STRNEWLINE " ; } $ mat = array ( array ( 1 , 0 , 1 ) , array ( 0 , 0 , 0 ) , array ( 1 , 0 , 1 ) ) ; checkHV ( $ mat , 3 , 3 ) ; ? >