Marc Harnist

implode
(PHP 4, PHP 5, PHP 7)

implode — Rassemble les éléments d'un tableau en une chaîne

Description
string implode ( string $glue , array $pieces )
string implode ( array $pieces )



$array = array('lastname', 'email', 'phone');
$comma_separated = implode(",", $array);

echo $comma_separated; // lastname,email,phone


LISTES A PUCES
$elements = array('a', 'b', 'c');
echo "< ul>< li>" . implode("< /li>< li>", $elements) . "< /li>< /ul>";



TABLEAUX!
function tablerow ($array)
{
    $rv = "< tr>< td>" . implode("< /td>< td>" , $array) . "< /td>< /tr>";
    return($rv);
}

function tablehead($array)
{
    $rv = "< tr>< th>" . implode("< /th>< th>" , $array) . "< /th>< /tr>";
    return($rv);
}

$db = mysqli_connect("localhost","user","password", "dbname");

$sql = "select id, name, city from users";
$query = mysqli_query($db, $sql);

while ($r  = mysqli_fetch_array($query, MYSQLI_ASSOC))
{
    if (!$keys)
        $keys = array_keys($r);
    $rows[] = tablerow($r);
}

$headers = tablehead($keys);
$rows = implode("", $rows);
echo "< table>" . $headers . $rows. "< /table>";