0

I am drawing numbers in arrays according to the number that the user chooses but I need to see them together in 2 horizontally linked arrays, but I do not get the result, this is my code:

function draw_numbers( $numbers ){

    $array_space = [
        ' ',
        ' ',
        ' ',
        ' ',
        ' ',
    ];
    // numbers
    $array_one = [
        [' ', ' ', '*',],
        [' ', ' ', '*',],
        [' ', ' ', '*',],
        [' ', ' ', '*',],
        [' ', ' ', '*',]
    ];
    $array_two = [
        ['*', '*', '*',],
        [' ', ' ', '*',],
        ['*', '*', '*',],
        ['*', ' ', ' ',],
        ['*', '*', '*',],
    ];

    $string_numbers = (string)$numbers;
    $array_numbers = str_split( $string_numbers );
    $array_results = [];
    if( is_array( $array_numbers ) ){
        foreach( $array_numbers as $num ){
            if( $num == 1 ){
                $array_results[] = $array_one;
            }elseif( $num == 2 ){
                $array_results[] = $array_two;
            }
        }
    }
    $array_results2 = [];
    $array_results3 = [];
    $array_results4 = [];
    if( ! empty( $array_results ) ){
        $k = 0;
        foreach( $array_results as $array_num ){
            foreach( $array_num as $array_num_row ){
                $array_results2[] = $array_num_row;
            }

            $array_results3 = [];
            foreach( $array_results2 as $row1 ){
                $array_results3 = array_merge( $array_results3, $row1 );
            }
            $array_results4[$k] = $array_results3;
            $k++;
        }
    }
    return $array_results4;
}

var_dump( draw_numbers(12) );

but I need the 2 arrangements to be joined horizontally and I can't get it

[[ , , * , , * , * , * ],
 [ , , * , ,   ,   , * ],
 [ , , * , , * , * , * ],
 [ , , * , , * ,   ,   ],
 [ , , * , , * , * , * ] ]

then the matrix should look like this..

matrix[0][0] = ' ';
matrix[0][1] = ' ';
matrix[0][2] = '*';
matrix[0][3] = ' ';
matrix[0][4] = '*';
matrix[0][5] = '*';
matrix[0][6] = '*';
matrix[1][0] = ' ';
matrix[1][1] = ' ';
matrix[1][2] = '*';
matrix[1][3] = ' ';
matrix[1][4] = ' ';
matrix[1][5] = ' ';
matrix[1][6] = '*';
matrix[2][0] = ' ';
matrix[2][1] = ' ';
matrix[2][2] = '*';
matrix[2][3] = ' ';
matrix[2][4] = '*';
matrix[2][5] = '*';
matrix[2][6] = '*';
matrix[3][0] = ' ';
matrix[3][1] = ' ';
matrix[3][2] = '*';
matrix[3][3] = ' ';
matrix[3][4] = '*';
matrix[3][5] = ' ';
matrix[3][6] = ' ';
matrix[4][0] = ' ';
matrix[4][1] = ' ';
matrix[4][2] = '*';
matrix[4][3] = ' ';
matrix[4][4] = '*';
matrix[4][5] = '*';
matrix[4][6] = '*';

What could I be doing wrong?

1 Answer 1

1

You can considerably simplify your code by putting all the picture arrays into a multidimensional array indexed by the digit being displayed. Also you can simplify the code by using the key of the number array to index into the result array when merging. Here's one way of doing that:

function add_digit(&$result, $digit) {
    // space
    $numbers[' '] = [
        [' '],
        [' '],
        [' '],
        [' '],
        [' '],
    ];
    // numbers
    $numbers['1'] = [
        [' ', ' ', '*',],
        [' ', ' ', '*',],
        [' ', ' ', '*',],
        [' ', ' ', '*',],
        [' ', ' ', '*',]
    ];
    $numbers['2'] = [
        ['*', '*', '*',],
        [' ', ' ', '*',],
        ['*', '*', '*',],
        ['*', ' ', ' ',],
        ['*', '*', '*',],
    ];

    foreach ($numbers[$digit] as $key => $value) {
        $result[$key] = array_merge($result[$key], $value);
    }
}

function draw_numbers( $value ){
    $result = array([],[],[],[],[]);
    foreach (str_split($value) as $i => $num) {
        // space between digits?
        if ($i > 0) add_digit($result, ' ');
        add_digit($result, $num);
    }
    return $result;
}

foreach (draw_numbers(12) as $row) {
    echo implode('', $row) ."\n";
}

Output:

  * ***
  *   *
  * ***
  * *  
  * ***

Demo on 3v4l.org

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.