Hello! I'm just starting to learn PHP help solve the problem)

On the village there is an input field in it, I have to write a key from the previously created array, and when I send it I should return the value of this key.

Here is an array:

$data = array( "Привет" => "Хай!", "Как дела?" => "Отлично!", ) 

Here is the form:

 require 'data.php'; // Подключаем массив $name = htmlspecialchars($_POST["text"]); $respond = "Error!"; if (($_POST["text"]) == "Сюда получаем ключ из input") { echo "Сюда приходит значение введенного ключа из массива"; } else { echo "$respond"; } <form method="post" action=""> <input type="text" name="text" id="text" placeholder="key"> <button type="submit">Send</button> </form> 

Maybe you have your options, how can this be implemented?

    2 answers 2

    Print the value by key:

     if(!empty($data[$name])) { echo $data[$name]; } else { echo $respond; } 
       if (isset($_POST["text"]) && array_key_exists($_POST["text"], $data)) { echo $data[$_POST["text"]]; } else { echo "$respond"; } 

      Documentation: https://www.php.net/manual/ru/function.array-key-exists.php