Exercise 3

Array, Loop


題目說明:

(1) 請建立一個陣列如下所示,並將它 print 出來

$arr = array('name' => 'Linwebs', 'city' => 'Chiayi', 'school' => 'ncyu');

(2) 請建立一個陣列,格式如下,並將它 print 出來,再利用 foreach 迴圈將每一項 print 出來,繪製成表格

$arr2 = array(
	array('name' => '蘋果', 'price' => 100),
	array('name' => '香蕉', 'price' => 20.5),
	array('name' => '芭樂', 'price' => 40.25)
);

註:以下預期結果 HTML 有經排版(增加空格以及換行),作答時無須進行排版,但仍要 print 出表格

預期結果
Array ( [name] => Linwebs [city] => Chiayi [school] => ncyu ) Array ( [0] => Array ( [name] => 蘋果 [price] => 100 ) [1] => Array ( [name] => 香蕉 [price] => 20.5 ) [2] => Array ( [name] => 芭樂 [price] => 40.25 ) )
id 名稱 價格
0 蘋果 100
1 香蕉 20.5
2 芭樂 40.25
2 芭樂 40.25
總共有 3 種水果
HTML
Array
(
    [name] => Linwebs
    [city] => Chiayi
    [school] => ncyu
)

Array
(
    [0] => Array
    (
        [name] => 蘋果
        [price] => 100
    )

    [1] => Array
    (
        [name] => 香蕉
        [price] => 20.5
    )

    [2] => Array
    (
        [name] => 芭樂
        [price] => 40.25
    )

)

<table>
	<tr>
		<th>id</th>
		<th>名稱</th>
		<th>價格</th>
	</tr>
	<tr>
		<td>0</td>
		<td>蘋果</td>
		<td>100</td>
	</tr>
	<tr>
		<td>1</td>
		<td>香蕉</td>
		<td>20.5</td>
	</tr>
	<tr>
		<td>2</td>
		<td>芭樂</td>
		<td>40.25</td>
	</tr>
	<tr>
		<td>2</td>
		<td>芭樂</td>
		<td>40.25</td>
	</tr>
	<tr>
		<td colspan="3">總共有 3 種水果</td>
	</tr>
</table>
在新視窗中開啟預期結果 開啟