Search in a row wise and column wise sorted matrix | Searches the element $x in mat [ ] [ ] . If the element is found , then prints its position and returns true , otherwise prints " not ▁ found " and returns false ; set indexes for top right element ; if $mat [ $i ] [ $j ] < $x ; if ( $i == $n $j == - 1 ) ; Driver Code
< ? php function search ( & $ mat , $ n , $ x ) { $ i = 0 ; $ j = $ n - 1 ; while ( $ i < $ n && $ j >= 0 ) { if ( $ mat [ $ i ] [ $ j ] == $ x ) { echo " n ▁ found ▁ at ▁ " . $ i . " , ▁ " . $ j ; return 1 ; } if ( $ mat [ $ i ] [ $ j ] > $ x ) $ j -- ; else $ i ++ ; } echo " n ▁ Element ▁ not ▁ found " ; return 0 ; } $ mat = array ( array ( 10 , 20 , 30 , 40 ) , array ( 15 , 25 , 35 , 45 ) , array ( 27 , 29 , 37 , 48 ) , array ( 32 , 33 , 39 , 50 ) ) ; search ( $ mat , 4 , 29 ) ; ? >