0

I'm trying to write a function that that returns an table from an array. But, for some reason, it is only displaying the first row or the array instead of all.

<html>
<link rel="stylesheet" type="text/css" href="week13.css" />
</head>
<body>
<h2>table</h2>
<br>
<br>
<?php
$table = array("440" => "cubic inch engine","truck F" => 150);
echo ("<table border='1'>\n");
function t6($table)
{
        foreach ($table as $key => $value)
        return ("<tr><td>$key</td><td>$value</td></tr>\n");
}
echo t6($table);
echo "</table>";
?>
<br>
</body>
</html>
2
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by tallygal
2

This is because you have a return statement within a foreach loop so the loop gets executed only once. Try this way:

function t6($table)
{
    $rows = '';

    foreach ($table as $key => $value) {

        $rows .= "<tr><td>$key</td><td>$value</td></tr>\n";
    }

    return $rows;
}

Edited by broj1

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.