how to loop over list of users in php

To loop over a list of users in PHP, you can use the foreach loop to iterate over each element of an array containing the user information. Here's an example:

main.php
// array of users
$users = array(
    array("name" => "John", "email" => "john@example.com"),
    array("name" => "Jane", "email" => "jane@example.com"),
    array("name" => "Bob", "email" => "bob@example.com"),
);

// loop over users
foreach ($users as $user) {
    echo "Name: " . $user["name"] . ", Email: " . $user["email"] . "<br>";
}
340 chars
12 lines

In this example, we first define an array of users, where each user is represented as an array containing their name and email. We then use the foreach loop to iterate over each user in the array and output their name and email. The loop assigns the current user to the $user variable on each iteration, which we can then access using array syntax ($user["name"] and $user["email"]). The output of this code would be:

main.php
Name: John, Email: john@example.com
Name: Jane, Email: jane@example.com
Name: Bob, Email: bob@example.com
106 chars
4 lines

related categories

gistlibby LogSnag