如何使用PHP解决此问题语句?

Suppose a number is given which is of positive integer type, e.g: 312. Please help me to write a program in PHP which convert that given number to a new number having same number of digits and all the digits of the new number must be equal to any of the digits of the given number (e.g: 333, 111, 222 by either decrementing or incrementing each digit by 1 at a time only). But print only that sequence of digits which takes lesser number of steps to generate the sequence and also print the number of steps taken to generate that sequence.

Explanation: Input: A positive integer N (e.g: 312)

converting the number(312) to the sequence of 3

3 2 2

3 3 2

3 3 3

here number of steps = 3

Now, converting the number(312) to the sequence of 1

2 1 2

1 1 2

1 1 1

here number of steps = 3

and finally converting the number(312) to the sequence of 2

2 1 2

2 2 2

here number of steps = 2

So, Output: 222

Number of steps: 2

Here's what I tried but lost

<?php

$num = 312;
$arr_num = array_map('intval', str_split($num));

//steps taken for each sequence will be stored in this array
$steps = array();

//printing number
for($i = 0; $i < count($arr_num); $i++)
    echo $arr_num[$i];


//calculation
for($i = 0; $i < count($arr_num); $i++) {
    $count = 0;

    for($j = 0; $j < count($arr_num); $j++) {

        if($arr_num[$i] == $arr_num[$j])
            ++$j;

        elseif($arr_num[$i] > $arr_num[$j]) {

            while($arr_num[$j] != $arr[$i]) {
                $arr_num[$j] += 1;
                $count++;
            }
        }

        else {
            while($arr_num[$j] != $arr_num[$i]) {
                $arr_num[$j] -= 1;
                $count++;
            }
        }
    }
    //pushing the count to steps array for each sequence
    array_push($steps, $count);

}
//I am stuck here...can't find the further solution
?>

This works (according to my very quick testing)):

 $intIn = 312;

# function changeDigits( $intIn ) { // uncomment for function
  $digits = str_split( $intIn ); // convert to array of digits
  $numerOfDigits = count($digits);
  $numberOfSteps = array();

 # check each digit in number
 for ($i=0; $i < $numerOfDigits; $i++) {
    $numberOfSteps[$i] = 0;
    $currentDigit = $digits[$i];

    # count the number of inc/decrements to change the other digits to this digit
    foreach($digits as $otherDigit) {
     if ($currentDigit > $otherDigit) $numberOfSteps[$i] += $currentDigit - $otherDigit;
     if ($currentDigit < $otherDigit) $numberOfSteps[$i] += $otherDigit - $currentDigit;
    }
  }
  $digitKey = array_search( min($numberOfSteps), $numberOfSteps );
  echo 'Number of Steps: ' . $numberOfSteps[$digitKey] . PHP_EOL;  // (or '<br>')
  echo 'New number = ' . str_repeat( $digits[$digitKey], $numerOfDigits );
 #}

# changeDigits(312);

Demo I devepled this code. please have a look once

<?php
    function find_output($input)
    {
        $digits = str_split($input);

        foreach ($digits as $index => $d) {
            $new_array = $digits;
            unset($new_array[$index]);
            $sum = 0;
            foreach ($new_array as $value) {
                $sum += abs($d - $value);
            }
            $final_array[$d] = $sum;
        }
        $steps = min($final_array);
        echo "steps : " . $steps . '<br>';
        $final_value = array_search(min($final_array), $final_array);
        echo "Output: " . implode(array_fill(0, count($digits), $final_value));
    }

    find_output(819);
    ?>

I think this will do the job.

<?php
$input = 312;
$input_array = [];
for($x=0;$x<3;$x++) {
    array_push($input_array,strval($input)[$x]);
}

function equalize($input_array, $mark) {
    $i = 0;
    for($x=0;$x<count($input_array);$x++) {
        $input_array[$x] = intval($input_array[$x]);
        #print($input_array[$x]);
        while($input_array[$x] != $mark) {
            if($input_array[$x] < $mark) {
                $input_array[$x] = $input_array[$x] + 1;
                $i++;
            }
            else {
                $input_array[$x] = $input_array[$x] - 1;
                $i++;
            }
        }
    }
    $output_val = intval($input_array[0] .$input_array[1] .$input_array[2]);
    return $output = [$output_val,$i];
}
#to first
$mark = $input_array[0];
$output = equalize($input_array, $mark);
#to second
$mark = $input_array[1];
$data = equalize($input_array, $mark);
if($data[1] < $output[1]) {
    $output = $data;
}
#to last
$mark = $input_array[2];
$data = equalize($input_array, $mark);
if($data[1] < $output[1]) {
    $output = $data;
}
echo 'Digit: ' .$output[0] .'<br/>';
echo 'Number of steps: ' .$output[1];
?>
<?php
class SeqSolver
{
    public function solve($str_num)
    {
        if(!ctype_digit($str_num))
            throw new Exception('Invalid input.  Input string must contain digits between 0 and 9 only.');

        $digits = str_split($str_num);
        $length = count($digits);

        foreach(array_unique($digits) as $digit)
            $results[$digit] = $this->stepsToSequence($str_num, $digit);

        //var_export($results);
        $min_keys = array_keys($results, min($results));

        // Prepare result
        $result['input'] = $str_num;
        foreach($min_keys as $key)
            $result['solutions'][] = [
                'sequence' => str_repeat($key, $length),
                'steps'    => $results[$key]
            ];

        return $result;
    }

    public function stepsToSequence($str_num, $target_digit) {
        $digits = str_split($str_num);
        $steps  = 0;
        foreach($digits as $digit)
            $steps += abs($digit - $target_digit);

        return $steps;
    }
}

Example use:

$solver = new SeqSolver;
foreach(['312', '334', '39'] as $input) {
    $result = $solver->solve($input);
    var_export($result);
    echo "
";
}

Output:

array (
  'input' => '312',
  'solutions' => 
  array (
    0 => 
    array (
      'sequence' => '222',
      'steps' => 2,
    ),
  ),
)
array (
  'input' => '334',
  'solutions' => 
  array (
    0 => 
    array (
      'sequence' => '333',
      'steps' => 1,
    ),
  ),
)
array (
  'input' => '39',
  'solutions' => 
  array (
    0 => 
    array (
      'sequence' => '33',
      'steps' => 6,
    ),
    1 => 
    array (
      'sequence' => '99',
      'steps' => 6,
    ),
  ),
)