-3

I'm trying to create a dictionary website and I came into some issues. I am printing the first value of array to the website. I'm trying to check if the input that is submitted into textbox is the same as translation of the word that is displayed at my website. If yes, then I need to remove that element from an array so that the first value of array now would be a second value from original array and so on, until the array is empty. After that, I would have an array with words that user didn't knew how to translate.

So my question is, how do I remove an element from array so that indexes wouldn't mess up? Or is there a better way to solve my problem?

<?php 
$servername = "localhost";
$username = "stud";
$password = "stud";
$dbname = "zodynas-eng";

//Connection
$conn = new mysqli($servername, $username, $password, $dbname);

//Check
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT * FROM politephrases";
$result = $conn->query($sql);

//Create an empty array
$array = array();

if ($result->num_rows > 0) {
// output data of each row
    while($row = $result->fetch_assoc()) {
        $array[] = $row;
    }
}

echo "<br>";

//shuffle($array);

echo "<h2>Word: </h2>";

$i = 0;
echo $array[$i]['word'];
echo '<form name = "tr" method = "post" action = "polite-phrases-eng.php">
        <br>
        <input type = "text" value = "" name = "translation">
        <input type = "submit" name = "submit1" value = "Vykdyti">
    </form>';
if(!empty($_POST)){
    if($array[$i]['transl'] == $_POST) {
        echo "<br> Good job";

        //Remove an element from array
    }
    else {
        //Save the element from input to wrongGuessArray
    }
}
else {
    echo "<br><p>Translation</p>";
}

$conn->close();
?>
Jakub Matczak
  • 15,341
  • 5
  • 46
  • 64
Benas
  • 1
  • 2
    *"So my question is, how do I remove an element from array so that indexes wouldn't mess up?"* -- use [`unset()`](http://php.net/manual/en/function.unset.php). And next time try to provide a [mcve]. – axiac Nov 21 '17 at 13:38
  • Already answered here, Duplicate of https://stackoverflow.com/questions/369602/php-delete-an-element-from-an-array/369608#369608 – Farsheel Nov 21 '17 at 13:40
  • Why you put blankspaces in `type = "submit"` – Daniel W. Nov 21 '17 at 13:40

1 Answers1

0

You can use unset or array_splice, for example :

$arr = array('a','b','c','d');
unset($arr[1]);

If you dont know the index or key you can use array_search to find an index or key of the arrays

user8455694
  • 260
  • 3
  • 12
  • That doesn't seem to remove it. When I put correct value and press submit page refreshes and the displayed value array[0] is the same as it was before. – Benas Nov 21 '17 at 13:52