配列に便利な関数05
添字と繰り返し文
- キーとして添字も出力できる
<!DOCTYPE HTML> <html lang="ja"> <head> <meta charset="UTF-8"> <title>foreach文で添字を出力する</title> </head> <body> <?php $product[0] = '鉛筆'; $product[1] = '消しゴム'; $product[2] = '定規'; $product[3] = 'コンパス'; $product[4] = 'ボールペン'; ?> <table border="1"> <tr><th>番号</th><th>商品名</th></tr> <?php foreach($product as $id => $value){ print ('<tr><td>' . $id . '</td><td>' . $value . '</td></tr>' . "\n"); } ?> </table> </body> </html>