This seems trivial but I'm baffled that I haven't been able to come to a solution on this. What I'm trying to do is:
Input -> 14025
Output -> 10245
Input -> 171
Output -> 117
And so on...
This seems trivial but I'm baffled that I haven't been able to come to a solution on this. What I'm trying to do is:
Input -> 14025
Output -> 10245
Input -> 171
Output -> 117
And so on...
$input = 140205;
$temp = str_split($input);
sort($temp);
// find the 1st non-zero digit
$f = current(array_filter($temp));
// remove it from the array
unset($temp[array_search($f, $temp)]);
echo $output = $f . join($temp); // 100245
A bit more verbose, but similar to previous answer
function smallestNumberFromDigits($string) {
//split string to digits
$array = str_split($string);
//sort the digits
sort($array);
//find the first digit larger than 0 and place it to the begining of array
foreach($array as $i => $digit) {
if($digit > 0) {
$tmp = $array[0];
$array[0] = $digit;
$array[$i] = $tmp;
break;
}
}
//return the imploded string back
return implode("", $array);
}