php 使用多维数组的键重建单词
php rebuild words using keys of a multidimensional array
我创建了一种组合一些“tiles”的方法来查找所有序列来组成一个单词。
<?php
/* convert word to tiles sequences */
function word2sequences($word, $tiles, &$founds = array()) {
foreach ($tiles as $tile_id => $tile) {
if (!preg_match("/^(|[;0-9]{1,})(".$tile.")(.*)$/i", $word, $tok)) continue;
$found = $tok[1].";".$tile_id.";".$tok[3];
if (!array_key_exists($word, $founds)) $founds[$tile] = array();
$founds[$tile] = word2sequences($found, $tiles);
}
return $founds;
}
/* example */
$tiles = [1 => "A", 21 => "B", 34 => "AH", 40 => "R", 51 => "S", 83 => "SA", 14 => "T", 21 => "TA"];
$word = "stars";
$sequences = word2sequences($word, $tiles);
echo "<pre>";
var_dump($sequences);
?>
输出是
array(1) {
["S"]=>
array(2) {
["TA"]=>
array(1) {
["R"]=>
array(1) {
["S"]=>
array(0) {
}
}
}
["T"]=>
array(1) {
["A"]=>
array(1) {
["R"]=>
array(1) {
["S"]=>
array(0) {
}
}
}
}
}
}
所以我知道要组成“星星”这个词我可以使用方块“S,TA,R,S”或“S,T,A,R,S”。
有一种方法可以遍历 $requences 数组,以简化格式提取所有序列,例如:
$sequences = array(
0 => array("S", "TA", "R", "S"),
1 => array("S", "T", "A", "R", "S")
)
提前发送。
我的解决方案是:更改 word2sequences() 函数。
<?php
/* convert word to tiles sequences */
function word2sequences($word, $tiles, &$founds = array()) {
foreach ($tiles as $tile_id => $tile) {
if (!preg_match("/^(|[;0-9]{1,})(".$tile.")(.*)$/i", $word, $tok)) continue;
$found = $tok[1].";".$tile_id.";".$tok[3];
if (preg_match("/^(|\s[;0-9]{1,}|[;0-9]{1,})$/i", $found)) $founds[] = $found;
else word2sequences($found, $tiles, $founds);
}
}
/* example */
$tiles = [1 => "A", 21 => "B", 34 => "AH", 40 => "R", 51 => "S", 83 => "SA", 14 => "T", 21 => "TA"];
$word = "stars";
$founds = array();
word2sequences($word, $tiles, $founds);
/* show tiles */
foreach ($founds as $key => $val) {
$val = preg_split("/;/", $val, -1, PREG_SPLIT_NO_EMPTY);
$solution = array();
foreach ($val as $tile => $sign_id) $solution[] = $tiles[$sign_id];
$founds[$key] = $solution;
}
print_r($founds);
?>
和输出:
Array
(
[0] => Array
(
[0] => S
[1] => TA
[2] => R
[3] => S
)
[1] => Array
(
[0] => S
[1] => T
[2] => A
[3] => R
[4] => S
)
)
我创建了一种组合一些“tiles”的方法来查找所有序列来组成一个单词。
<?php
/* convert word to tiles sequences */
function word2sequences($word, $tiles, &$founds = array()) {
foreach ($tiles as $tile_id => $tile) {
if (!preg_match("/^(|[;0-9]{1,})(".$tile.")(.*)$/i", $word, $tok)) continue;
$found = $tok[1].";".$tile_id.";".$tok[3];
if (!array_key_exists($word, $founds)) $founds[$tile] = array();
$founds[$tile] = word2sequences($found, $tiles);
}
return $founds;
}
/* example */
$tiles = [1 => "A", 21 => "B", 34 => "AH", 40 => "R", 51 => "S", 83 => "SA", 14 => "T", 21 => "TA"];
$word = "stars";
$sequences = word2sequences($word, $tiles);
echo "<pre>";
var_dump($sequences);
?>
输出是
array(1) {
["S"]=>
array(2) {
["TA"]=>
array(1) {
["R"]=>
array(1) {
["S"]=>
array(0) {
}
}
}
["T"]=>
array(1) {
["A"]=>
array(1) {
["R"]=>
array(1) {
["S"]=>
array(0) {
}
}
}
}
}
}
所以我知道要组成“星星”这个词我可以使用方块“S,TA,R,S”或“S,T,A,R,S”。
有一种方法可以遍历 $requences 数组,以简化格式提取所有序列,例如:
$sequences = array(
0 => array("S", "TA", "R", "S"),
1 => array("S", "T", "A", "R", "S")
)
提前发送。
我的解决方案是:更改 word2sequences() 函数。
<?php
/* convert word to tiles sequences */
function word2sequences($word, $tiles, &$founds = array()) {
foreach ($tiles as $tile_id => $tile) {
if (!preg_match("/^(|[;0-9]{1,})(".$tile.")(.*)$/i", $word, $tok)) continue;
$found = $tok[1].";".$tile_id.";".$tok[3];
if (preg_match("/^(|\s[;0-9]{1,}|[;0-9]{1,})$/i", $found)) $founds[] = $found;
else word2sequences($found, $tiles, $founds);
}
}
/* example */
$tiles = [1 => "A", 21 => "B", 34 => "AH", 40 => "R", 51 => "S", 83 => "SA", 14 => "T", 21 => "TA"];
$word = "stars";
$founds = array();
word2sequences($word, $tiles, $founds);
/* show tiles */
foreach ($founds as $key => $val) {
$val = preg_split("/;/", $val, -1, PREG_SPLIT_NO_EMPTY);
$solution = array();
foreach ($val as $tile => $sign_id) $solution[] = $tiles[$sign_id];
$founds[$key] = $solution;
}
print_r($founds);
?>
和输出:
Array
(
[0] => Array
(
[0] => S
[1] => TA
[2] => R
[3] => S
)
[1] => Array
(
[0] => S
[1] => T
[2] => A
[3] => R
[4] => S
)
)