Php - 字符串到 3 维数组
Php - string to array 3 dimensions
我来找你是为了解决我的问题:
我有一个像这样的扁弦:
$chaine = "519637824467582931832419576721894365346125789985376412698243157273951648154768293";
我要转换这个维数组链,这里是这个格式:
$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],
];
我的初始回复是 $ grille_splited = str_split ($ string, 9);
我得到了 table:
$tab = [
[000000000],
[000000000],
[000000000],
[000000000],
[000000000],
[000000000],
[000000000],
[000000000],
[000000000],
];
我错过了一个我相信的维度?
供参考:此题旨在使用求解器数独 (Sudoku Projet)
*google 翻译的结果
编辑:抱歉 Guy 和 Thx 的提示! :)
您需要对 9 个字符的字符串执行额外的拆分:
$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
)
)/*
我来找你是为了解决我的问题:
我有一个像这样的扁弦:
$chaine = "519637824467582931832419576721894365346125789985376412698243157273951648154768293";
我要转换这个维数组链,这里是这个格式:
$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],
];
我的初始回复是 $ grille_splited = str_split ($ string, 9);
我得到了 table:
$tab = [
[000000000],
[000000000],
[000000000],
[000000000],
[000000000],
[000000000],
[000000000],
[000000000],
[000000000],
];
我错过了一个我相信的维度?
供参考:此题旨在使用求解器数独 (Sudoku Projet)
*google 翻译的结果
编辑:抱歉 Guy 和 Thx 的提示! :)
您需要对 9 个字符的字符串执行额外的拆分:
$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
)
)/*