Search code examples
phparraysstringsudokustrsplit

Php - string to array 3 dimensions


I come to you to find a solution to my problem:

I have a flat string like this:

$chaine = "519637824467582931832419576721894365346125789985376412698243157273951648154768293";

I want to convert this dimensional array chain, this format here:

$tab = [
    [0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0],
];

I had an initial response with $ grille_splited = str_split ($ string, 9); I get a table:

$tab = [
[000000000],
[000000000],
[000000000],
[000000000],
[000000000],
[000000000],
[000000000],
[000000000],
[000000000],
];

I'm missing a Dimension I believe?

For information: this question aims to use a Solver Sudoku (Sudoku Projet)

*result of a google translate

Edit : Sorry Guy's and Thx for your tips ! :)


Solution

  • You need to perform an aditional split on the 9 char strings:

    $string = "519637824467582931832419576721894365346125789985376412698243157273951648154768293";
    $output = array_map(function($element){
        return str_split($element);
    }, str_split($string, 9));
    
    /*$output equals
    Array
    (
        [0] => Array
            (
                [0] => 5
                [1] => 1
                [2] => 9
                [3] => 6
                [4] => 3
                [5] => 7
                [6] => 8
                [7] => 2
                [8] => 4
            )
    
        [1] => Array
            (
                [0] => 4
                [1] => 6
                [2] => 7
                [3] => 5
                [4] => 8
                [5] => 2
                [6] => 9
                [7] => 3
                [8] => 1
            )
    
        [2] => Array
            (
                [0] => 8
                [1] => 3
                [2] => 2
                [3] => 4
                [4] => 1
                [5] => 9
                [6] => 5
                [7] => 7
                [8] => 6
            )
    
        [3] => Array
            (
                [0] => 7
                [1] => 2
                [2] => 1
                [3] => 8
                [4] => 9
                [5] => 4
                [6] => 3
                [7] => 6
                [8] => 5
            )
    
        [4] => Array
            (
                [0] => 3
                [1] => 4
                [2] => 6
                [3] => 1
                [4] => 2
                [5] => 5
                [6] => 7
                [7] => 8
                [8] => 9
            )
    
        [5] => Array
            (
                [0] => 9
                [1] => 8
                [2] => 5
                [3] => 3
                [4] => 7
                [5] => 6
                [6] => 4
                [7] => 1
                [8] => 2
            )
    
        [6] => Array
            (
                [0] => 6
                [1] => 9
                [2] => 8
                [3] => 2
                [4] => 4
                [5] => 3
                [6] => 1
                [7] => 5
                [8] => 7
            )
    
        [7] => Array
            (
                [0] => 2
                [1] => 7
                [2] => 3
                [3] => 9
                [4] => 5
                [5] => 1
                [6] => 6
                [7] => 4
                [8] => 8
            )
    
        [8] => Array
            (
                [0] => 1
                [1] => 5
                [2] => 4
                [3] => 7
                [4] => 6
                [5] => 8
                [6] => 2
                [7] => 9
                [8] => 3
            )
    
    )/*