Arrange given numbers to form the smallest number | Utility function to print the contents of an array ; A comparison function that return true if ' AB ' is smaller than ' BA ' when we concatenate two numbers ' A ' and ' B ' For example , it will return true if we pass 12 and 24 as arguments . This function will be used by sort ( ) function ; Convert first number to string format ; Convert second number to string format ; Check if ' AB ' is smaller or ' BA ' and return bool value since comparison operator ' < = ' returns true or false ;  ; Function to print the arrangement with the smallest value ; If we pass the name of the comparison function it will sort the array according to the compare function ; Print the sorted array ; Driver code
< ? php function printArr ( $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr [ $ i ] ; } function compare ( $ num1 , $ num2 ) { $ A = ( string ) $ num1 ; $ B = ( string ) $ num2 ; if ( ( int ) ( $ A . $ B ) <= ( int ) ( $ B . $ A ) ) { return true ; } else return false ; } function sort_arr ( $ arr ) { for ( $ i = 0 ; $ i < count ( $ arr ) ; $ i ++ ) { for ( $ j = $ i + 1 ; $ j < count ( $ arr ) ; $ j ++ ) { if ( compare ( $ arr [ $ i ] , $ arr [ $ j ] ) == false ) { $ temp = $ arr [ $ i ] ; $ arr [ $ i ] = $ arr [ $ j ] ; $ arr [ $ j ] = $ temp ; } } } return $ arr ; } function printSmallest ( $ N , $ arr ) { $ arr = sort_arr ( $ arr ) ; printArr ( $ arr , $ N ) ; } $ arr = array ( 5 , 6 , 2 , 9 , 21 , 1 ) ; $ N = count ( $ arr ) ; printSmallest ( $ N , $ arr ) ; ? >