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 . ; lower_bound returns iterator pointing to first element that does not compare less to x . ; If x is not present return - 1. ; Code driven
< ? php function findFirst ( $ arr , $ n , $ x ) { sort ( $ arr ) ; $ ptr = floor ( $ arr ) ; return ( $ ptr != $ x ) ? 1 : ( $ ptr - $ arr ) ; } $ x = 20 ; $ arr = array ( 10 , 30 , 20 , 50 , 20 ) ; $ n = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; echo findFirst ( $ arr , $ n , $ x ) ; #This  code is contributed by Tushil. NEW_LINE ? >