0

I'm doing a school assignment where we need to "translate" the input in the textarea to the pre-written words in the arrays. I don't know how to make the choosen language radio array to search for the language array.

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.
?>

If there is anything that I've done anything wrong in the steps before please tell me so I can learn how to improve. I've tried creating arrays within arrays but that just gives me more errors.

2 Answers 2

3

You can use the $$ mechanism to use a variable to address a variable

$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];

RESULT

1Pomme
Sign up to request clarification or add additional context in comments.

Comments

2

To create a multidimensional array of the different languages, you could use something like

$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" ),

);

Then use the $choosenLanguage variable as the dimension of the array to use...

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

1 Comment

@wanted9 Do not be tempted by the "shiny thing" that is "variable variables". Use Nigel's answer to avoid developing a bad coding habit. When I first encountered variable variables, I thought that they were totally amazing. Then I learned that you are effectively casting array-like data as non-array data. This means you lose the ability to use php's powerful array functions AND your dynamic variables will be harder to trace (by human developers and your IDE). Set up your array data as Nigel has demonstrated, then pat yourself on the back for using a professional technique.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.