Im Folgenden finden Sie meinen Code für ein mehrdimensionales Array, um ihn als Tabelle mit Farben der alternativen Zeilen anzuzeigen.
<?php
$Mags = array(
array(
"1", "Krish","1977/03","V10", "M3","March-77"
),
array(
"2", "Vansh","1978/09","V11", "M9","Sept-78"
),
array(
"3", "Sushi","1981/07","V14", "M7","July-81"
)
);
?>
<table id="fancytable"><tbody><thead>
<tr>
<th>S.No</th>
<th>Dedicate</th>
<th>Vol.</th>
<th>ID</th>
<th>Month-Year</th>
</tr></thead>
<?php
$i=1;
foreach($Mags as $mag)
{
if($i % 2 == 0)
{
echo "<tr class="odd">";
}
else
{
echo "<tr>";
}
foreach($mag as $item)
{
echo "<td>$item</td>";
}
echo "</tr>";
$i = $i + 1;
}
?>
</tbody></table>
Ich möchte nicht für jedes Element einen separaten "Schlüssel" angeben. weil ich Array-Elemente aus einer Excel-CSV-Datei kopiere (d. h. jede Zeile mit 6 Spalten) Ich möchte nur fünfmal pro Zeile zeigen. Artikel Nr. "3" sollte als relative URL (mit Basis-URL als "http://mydomain.com/") für Artikel Nr. "2" verwendet werden.
Meine Frage ist : Wie muss ich die php-Funktion "foreach" verwenden?
Antworten:
0 für die Antwort № 1Sie können dies nicht tun. Verwenden Sie einfach eine reguläre for-Schleife:
for ($x = 0; $x < count($Mags); $x++) {
if (isset($Mags[$x - 1])) {
$Mags[$x]["hyperlink"] = $Mags[$x - 1];
}
}