Home > OS >  How to display a PHP multidimensional associative array with a while loop, a pointer and the current
How to display a PHP multidimensional associative array with a while loop, a pointer and the current

Time:10-01

Good evening everyone,

Firstly, I would want to precise I’m new in the community. So I thank you to excuse-me concerning potential mistakes from my part.

Today I come to you because within the scope of my studies I must achieve a multidimensional associative array thank to PHP and display it thank a while loop, a pointer and the current() function.

I was able to create without any problems the array I'm asked to do, but I’m stuck to display this one.

You’ll find following the code I already achieved. The name of each variable was originally in French, but I did a translation effort for you haven’t to meet this complex language.

    <style type="text/css">
    *
    {
        margin: unset;
        box-sizing: border-box;
        font-family: arial;
    }

    table
    {
        margin: 0px 0px 0px 10px;
        border-collapse: collapse;
    }

    .col
    {
        border: 1px solid black;
        padding: 5px 10px 5px 10px;
        font-size: 10pt;
    }

    th
    {
        font-size: 12pt !important;
        color: white;
        background-color: orange;
        text-align: center;
    }
</style>
<?php
    // Variables declaration
    $users = array(
        "pseudo1" => array("name"=>"Edouard", "firstname"=>"Louis", "email"=>"[email protected]", "passwd"=>hash("md5", "Road66")),
        "pseudo2" => array("name"=>"Edouard", "firstname"=>"Louis", "email"=>"[email protected]", "passwd"=>hash("md5", "Road66")),
        "pseudo3" => array("name"=>"Edouard", "firstname"=>"Louis", "email"=>"[email protected]", "passwd"=>hash("md5", "Road66")),
        "pseudo4" => array("name"=>"Edouard", "firstname"=>"Louis", "email"=>"[email protected]", "passwd"=>hash("md5", "Road66"))
    );

    // To verify that created array contains the right elements
    var_dump($users);
?>
<br>
<br>
<table>
    <tr>
        <th >Name</th>
        <th >Firstname</th>
        <th >Email address</th>
        <th >Password</th>
    </tr>
<?php
// Reset of pointer
reset($utilisateurs);

while (current($utilisateurs))
{
?>
    <tr>
        <td ><?php echo(…); ?></td>
        <td ><?php echo(…); ?></td>
        <td ><?php echo(…); ?></td>
        <td ><?php echo(…); ?></td>
    </tr>
<?php
    next($utilisateurs);
}
?>
</table>

CodePudding user response:

The current function returns the current item or false if there's no more items. So you can assign that to a variable and use in your echo.

// Variables declaration
$users = array(
    "pseudo1" => array("name"=>"Edouard", "firstname"=>"Louis", "email"=>"[email protected]", "passwd"=>hash("md5", "Road66")),
    "pseudo2" => array("name"=>"Alice", "firstname"=>"Louis", "email"=>"[email protected]", "passwd"=>hash("md5", "Road66")),
    "pseudo3" => array("name"=>"Bob", "firstname"=>"Louis", "email"=>"[email protected]", "passwd"=>hash("md5", "Road66")),
    "pseudo4" => array("name"=>"Charlie", "firstname"=>"Louis", "email"=>"[email protected]", "passwd"=>hash("md5", "Road66"))
);

// Reset of pointer
reset($users);

while ($currentUser = current($users))
{
    var_dump($currentUser);
    next($users);
}

Demo: https://3v4l.org/hFAG0

Also, I'm hoping this is for educational purposes only because there are other constructs that are way less confusing and more common, specifically foreach

  • Related