0

For a assignment I am asked to create an array of some users. (At least 5) I need to apply the For-Each structure but I don't understand what I'm doing wrong in my code since I only get the last value.

I hope someone can explain to me what I'm doing wrong and how I can solve this problem. My apologies in advance if I'm doing something wrong with the ToS...

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>13-FOR-EACH controle structuren</title>
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="">
    </head>
    <body>
        <?php

        $user = array(
                "Gebruikersnaam" => "OJ", "Wachtwoord" => "Aapje@123", "Email" => "info@officialjens.be",
                "Gebruikersnaam" => "Wolf", "Wachtwoord" => "Wolfje@123", "Email" => "wolf@wolftm.nl",
                "Gebruikersnaam" => "PietjePuk", "Wachtwoord" => "Pukje@123", "Email" => "help@pietpjepuk.eu",
                "Gebruikersnaam" => "Aquila", "Wachtwoord" => "Aquila@123", "Email" => "info@aquila.org",
                "Gebruikersnaam" => "CS", "Wachtwoord" => "CS@123", "Email" => "info@creationlystudio.com");

        print "<ol>";
        
        foreach ($user as $key => $value) {
            print "<li>$key : $value</li>";
        }

        print "</ol>";
        ?>
    </body>
</html>

What is expected in this assignment is that it print all 5 users.

Exoisme
  • 9
  • 2
  • 3
    Even though the users are separate lines, they aren't separate values, so you've overwriting the values. You need to turn that into a multidimensional array, so that each user is it's own child array – aynber Nov 22 '22 at 16:21
  • 1
    Allright, thanks for the information @aynber! Do you have any example? – Exoisme Nov 22 '22 at 16:24
  • 1
    `[["Gebruikersnaam" => "OJ", "Wachtwoord" => "Aapje@123", "Email" => "info@officialjens.be"], ["Gebruikersnaam" => "Wolf", "Wachtwoord" => "Wolfje@123", "Email" => "wolf@wolftm.nl"], ...]` – Barmar Nov 22 '22 at 16:25
  • 1
    `array(array('key1'=>1, 'key2'=>2,'key3'=>3),array('key1'=>1, 'key2'=>2,'key3'=>3),array('key1'=>1, 'key2'=>2,'key3'=>3))` or bracket syntax is sometimes easier, `[['key1'=>1, 'key2'=>2,'key3'=>3],['key1'=>1, 'key2'=>2,'key3'=>3],['key1'=>1, 'key2'=>2,'key3'=>3]]` – aynber Nov 22 '22 at 16:25
  • 3
    Further, `$user` implies singular, so `$users` is probably more appropriate. You'd then `foreach` over `$users` to get the individual `$user`, and then `foreach` over that user with your existing code to get the properties. – Chris Haas Nov 22 '22 at 16:29

0 Answers0