Search , insert and delete in an unsorted array | To search a key to be deleted ; Function to delete an element ; Find position of element to be deleted ; Deleting element ; Driver code
< ? php function findElement ( & $ arr , $ n , $ key ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ arr [ $ i ] == $ key ) return $ i ; return -1 ; } function deleteElement ( & $ arr , $ n , $ key ) { $ pos = findElement ( $ arr , $ n , $ key ) ; if ( $ pos == -1 ) { echo " Element ▁ not ▁ found " ; return $ n ; } for ( $ i = $ pos ; $ i < $ n - 1 ; $ i ++ ) $ arr [ $ i ] = $ arr [ $ i + 1 ] ; return $ n - 1 ; } $ arr = array ( 10 , 50 , 30 , 40 , 20 ) ; $ n = count ( $ arr ) ; $ key = 30 ; echo " Array ▁ before ▁ deletion STRNEWLINE " ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; $ n = deleteElement ( $ arr , $ n , $ key ) ; echo " Array after deletion " ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; ? >