【问题标题】:php two dimensional array permutation...; single phonics have multiple letters in englishphp 二维数组排列...;单拼音有多个英文字母
【发布时间】:2019-12-09 18:15:36
【问题描述】:

我有一系列具有多个备用英文字母的拼音

$q[0][0]="c";
$q[1][0]="k";
$q[2][0]="q";

---
$q[0][1]="u";
$q[1][1]="a";

--

$q[0][2]="t"

我有包含单词的单词表 切 放 标记

我想置换上面的二维数组并生成包含所有可能单词的查询

cut
qat
kut
--
cat
qut
kat
----

或者我们也可以包括并继续上述系列的程序逻辑要求

tac
tuc
tak
---
tuq
taq
tuk
---
act
ukt
uct
---

我只需要像 $word{$i] 这样的数组变量,它具有所有上述可能的排列词。 当我在查询可能的单词上运行时,它只会在表中找到“cut”,我会将“ڪَٽ”替换为“cut”……是的,它的罗马音译 我还没有尝试过的编程是

<input type="text" name="leng" id="leng" value="" ><input type='submit' name='submit'></form>
<?php 
if(isset($_POST['submit'])){

    for($i=0; $i<=3; $i++){ //3 is number of listboxes or length of given string

        //echo $list[$i]; 

        for ($x=0; $x<= $_POST['leng']; $x++){
?>
<script>
document.getElementById("leng").value = document.getElementById("<?php echo $list[$i]; ?>").length;
 </script>

// 我在上面做了三个列表框,分别命名为list0、list1、list3 线 list0="c, q, k" list1="你,一个" list2="t"

 <?php
            //echo "($i-i)--  ($x-X) <BR>";
            echo $q[$i][$x]."<BR>";
        }
    }

【问题讨论】:

    标签: php combinations permutation


    【解决方案1】:

    这是我尝试过的完整程序.... 问题是“如果有人将“fahad”字符串写成英文,那么我不得不将其音译成信德语,而在信德语中,单个英文字母有多个替代项,例如 f 有 2 个替代项,a 有 3 个替代项 َ، آ، ا并且 h 有三个替换 ه، ح، ھ 并且 d 有三个替换 د، ڊ، ڏ 所有的替代品都被写入数据库表中针对英文字母......首先我将每个字母搜索到字母替代表中......如果找到,我将一个一个地放入列表/选择框......所有列表框都是等于“fahad”的长度数,因此生成了 5 个列表框..... 并将当前列表框的长度放入 $_POST['leng'] 文本框..... 现在我在这个 permutation of multidimensional array 上对 $q 数组进行排列

    然后我计算每5个字长后

    global $servername;
        global $username;
        global $password;
        global $dbname;
    
    
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "tsrs";
    //////////////////////
    
    global $q;
    $q=array();
    
    //////////////////////
    for ($n=0; $n<=strlen("fahad"); $n++){
    
    $i=substr("fahad", $n, 1);
    //echo $i;
    
    /////////////////////
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
       die("Connection failed: " . $conn->connect_error);
    } 
    mysqli_set_charset($conn,"utf8");
    $sql = "SELECT * FROM duplicate WHERE engletter='$i' order by id";
    $result = $conn->query($sql);
    global $m;
    $m=0;
    
    if ($result->num_rows > 0) {
    $altrntcount=$result->num_rows; 
    
    
        while($row = $result->fetch_assoc()) {
        $q[$n][$m]=$row['sinletter']    ;
        echo $q[$n][$m]."<BR>";
    
        $m=$m+1;
        }
        }
    
    }
    echo "<form method='post'>";
    
    for ($n=0; $n<=strlen("fahad")-1; $n++){
    $list[$n]="list".$n;
    echo $list[$n];
    echo "
    <select name=\"".$list[$n]."\" id=\"".$list[$n]."\">";
    
    for ($m=0; $m<=$altrntcount-1; $m++){
    if ($q[$n][$m]<>""){
    echo "<option value=\"".$q[$n][$m]."\">".$q[$n][$m]."</option>";
    
    }
    
    }
    echo "</select>";  
    }?> 
    <input type="text" name="leng" id="leng" value="1" ><input type='submit' name='submit'></form>
    <?php 
    if(isset($_POST['submit'])){
    
    for($i=0; $i<=strlen("fahad")-1; $i++){
    
        //echo $list[$i]; 
    
    
    
    
    
    for ($x=0; $x<= $_POST['leng']+1; $x++){
    
        ?>
    
        <script>
    
    
    
        document.getElementById("leng").value = document.getElementById("<?php echo $list[$i]; ?>").length;
    
     </script>
     <?php
    
        echo $i."-i: I : ".$x.": X <BR>";
    
        echo $q[$i][$x];
    
    }
    
    }
    echo "<h1>".($_POST['leng'])."zzz</h1>";
    
    
    
    
    
    
    
    echo "-----<BR><BR><BR>++++++";
    
    
    
    }
        ?>
    
    <?php
    global $q;
    
    
    //$arrStart = array(
     //array('ف', 'ڦ', 'په'),
       // array('ح', 'ه'),
        //array('د', 'ڊ','ڏ')
    //);
    
    //$arrStart= $q[$i][$x];
    $arrPositions =     array();
    $arrResult = array();
    
    //get a starting position set for each sub array
    for ($i = 0; $i < count($q); $i++)
        $arrPositions[] = 0;
    
    //repeat until we've run out of items in $q[0]
    while (array_key_exists($arrPositions[0], $q[0])) {
        $arrTemp = array();
        $blSuccess = true;
    
        //go through each of the first array levels
        for ($i = 0; $i < count($q); $i++) {
            //is there a item in the position we want in the current array?
            if (array_key_exists($arrPositions[$i], $q[$i])) {
                //add that item to our temp array
                $arrTemp[] = $q[$i][$arrPositions[$i]];
            } else {
                //reset this position, and raise the one to the left
                $arrPositions[$i] = 0;
                $arrPositions[$i - 1]++;
                $blSuccess = false;
            }
        }
    
        //this one failed due to there not being an item where we wanted, skip to next go
        if (!$blSuccess) continue;
    
        //successfully adding nex line, increase the right hand count for the next one
        $arrPositions[count($q) - 1]++;
    
        //add our latest temp array to the result
        $arrResult[] = $arrTemp;
    
    }
    
    
    
        print_r($arrResult);
    
    
    
    
    
        if(isset($_POST['submit'])){
    
    for($i=0; $i<=count($arrResult)-1; $i++){
    
        //echo $list[$i]; 
    
    
    ?>
    
        <script>
    
    
    
        document.getElementById("leng").value = document.getElementById("<?php echo $list[$i]; ?>").length;
    
     </script>
     <?php  
    
    for ($x=0; $x<= $_POST['leng']+2; $x++){
    
    
    
    
    
    
        //echo $i."-i: I : ".$x.": X <BR><BR><BR>";
    
        echo $arrResult[$i][$x];
    
    }
    echo "<BR><BR>";
    }
    echo "<h1>".($_POST['leng'])."zzz</h1>";
    
    
    
    
    
    
    
    echo "-----<BR><BR><BR>++++++";
    
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 2020-04-01
      • 1970-01-01
      • 2016-10-02
      • 1970-01-01
      • 1970-01-01
      • 2021-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多