【问题标题】:Seach Array with an array使用数组搜索数组
【发布时间】:2021-02-22 14:38:32
【问题描述】:

我正在做一项学校作业,我们需要将 textarea 中的输入“翻译”为数组中预先写好的单词。我不知道如何使选择的语言单选数组来搜索语言数组。

HTML

<html>
  <head>
    <title>
      Translation PHP
    </title>
  </head>
  <body>
    <h1>Input your word and select the language you want to translate to.</h1>

    <form action="form.php" method="GET/POST"> 
      <p>Input your word for translation:</p>
        <input type="text" name="inputword"> 

      <p>Select language to translate to:</p>
        <input type="radio" name="language" value="Swedish">Swedish<br>
        <input type="radio" name="language" value="Italian">Italian<br>
        <input type="radio" name="language" value="Spanish">Spanish<br>
        <input type="radio" name="language" value="German">German<br>
        <input type="radio" name="language" value="French">French<br>
        <input type="submit" name="translate" value="Translate"/>
    </form>
      <br>
      <p>Words available: Hello, Apple, Fruit, Car, Cat, Dog, Shoes, House, School, Sweatshirt</p>
  </body>
</html>

PHP

<?php 
$TranslateWord = $_GET['inputword'];
$choosenLanguage = $_GET['language'];

$English = array("Hello","Apple","Fruit","Car","Cat","Dog","Shoes","House","School","Sweatshirt");
$German = array("Hallo","Apfel","Obst","Wagen","Katze","Hund","Schuhe","Haus","Schule","Sweatshirt" );
$Swedish = array("Hej","Äpple","Frukt","Bil","Katt","Hund","Skor","Hus","Skola","Tröja");
$Italian = array("Ciao","Mela","Frutta","Macchina","Gatto","Cane","Scarpe","Casa","Scuola","Maglione");
$Spanish = array("Hola","Manzana","Fruta","Auto","Gato","Pero","Casa","Zapatos","Colegio","Sueter");
$French = array("Bonjour","Pomme","Fruit","Auto","Chat","Chien","Loger","Chaussures","l'ecole","Chandail");
    
    
    
$positionWord = array_search($TranslateWord, $English);
    
//This is where I need the language array to be found by the positionWord and choosen language radio in the form.
?>

如果我在之前的步骤中做错了什么,请告诉我,以便我学习如何改进。我尝试在数组中创建数组,但这只会给我更多错误。

【问题讨论】:

    标签: php arrays arraylist


    【解决方案1】:

    您可以使用$$ 机制来使用变量来寻址变量

    $TranslateWord = 'Apple';
    $choosenLanguage = 'French';
    
    $English = array("Hello","Apple","Fruit","Car","Cat","Dog","Shoes","House","School","Sweatshirt");
    $German = array("Hallo","Apfel","Obst","Wagen","Katze","Hund","Schuhe","Haus","Schule","Sweatshirt" );
    $Swedish = array("Hej","Äpple","Frukt","Bil","Katt","Hund","Skor","Hus","Skola","Tröja");
    $Italian = array("Ciao","Mela","Frutta","Macchina","Gatto","Cane","Scarpe","Casa","Scuola","Maglione");
    $Spanish = array("Hola","Manzana","Fruta","Auto","Gato","Pero","Casa","Zapatos","Colegio","Sueter");
    $French = array("Bonjour","Pomme","Fruit","Auto","Chat","Chien","Loger","Chaussures","l'ecole","Chandail");
        
    $pos = array_search($TranslateWord, $English);
    
    echo $pos;
    
    echo $$choosenLanguage[$pos];
    

    结果

    1Pomme
    

    【讨论】:

      【解决方案2】:

      要创建不同语言的多维数组,您可以使用类似

      $translate = array(
          "English" => array("Hello","Apple","Fruit","Car","Cat","Dog","Shoes","House","School","Sweatshirt"),
          "German" => array("Hallo","Apfel","Obst","Wagen","Katze","Hund","Schuhe","Haus","Schule","Sweatshirt" ),
      
      );
      

      然后使用$choosenLanguage变量作为数组的维度来使用...

      $positionWord = array_search($TranslateWord, $translate[$choosenLanguage]);
      

      【讨论】:

      • @wanted9 不要被“变量”这个“闪亮的东西”所诱惑。使用奈杰尔的答案来避免养成不良的编码习惯。当我第一次遇到可变变量时,我认为它们非常神奇。然后我了解到您正在有效地将类似数组的数据转换为非数组数据。这意味着您将失去使用 php 强大的数组函数的能力,并且您的动态变量将更难追踪(由人类开发人员和您的 IDE)。像 Nigel 演示的那样设置您的阵列数据,然后为自己使用专业技术而庆幸自己。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-27
      • 1970-01-01
      • 2012-09-16
      • 2011-09-12
      • 2015-07-02
      相关资源
      最近更新 更多