Find index of first occurrence when an unsorted array is sorted | PHP program to find index of first occurrence of x when array is sorted . ; Driver Code
< ? php function findFirst ( $ arr , $ n , $ x ) { $ count = 0 ; $ isX = false ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] == $ x ) $ isX = true ; else if ( $ arr [ $ i ] < $ x ) $ count ++ ; } return ( $ isX == false ) ? -1 : $ count ; } $ x = 20 ; $ arr = array ( 10 , 30 , 20 , 50 , 20 ) ; $ n = sizeof ( $ arr ) ; echo findFirst ( $ arr , $ n , $ x ) ; ? >