$stu = array('吳同學', '王同學', '林同學');
$stu = ['吳同學', '王同學', '林同學'];
$stu[] = '吳同學';
$stu[] = '王同學';
$stu[] = '林同學';
$stu = array(0 => '吳同學', 1 => '王同學', 2 => '林同學');
$stu = [0 => '吳同學', 1 => '王同學', 2 => '林同學'];
$stu[0] = '吳同學';
$stu[1] = '王同學';
$stu[2] = '林同學';
若 echo $stu[2]
會得到「林同學」三個字
$stu = array(1 => '吳同學', 2 => '王同學', 4 => '林同學');
$stu = [1 => '吳同學', 2 => '王同學', 4 => '林同學'];
$stu[1] = '吳同學';
$stu[2] = '王同學';
$stu[4] = '林同學';
若 echo $stu[2]
會得到「王同學」三個字
$stu = array('1號' => '吳同學', '2號' => '王同學', '4號' => '林同學');
$stu = ['1號' => '吳同學', '2號' => '王同學', '4號' => '林同學'];
$stu['1號'] = '吳同學';
$stu['2號'] = '王同學';
$stu['4號'] = '林同學';
若 echo $stu[2]
會得到空值,echo $stu['2號']
才會得到「王同學」三個字$stu
的值為上面所設):
$class_stu['甲班'] = $stu;
$class_stu['甲班'] = [1 => '吳同學', 2 => '王同學', 4 => '林同學'];
echo $class_stu['甲班']
會印不出來,顯示 Array,因為底下有好幾個,不曉得要顯示誰(可用 foreach
一個一個印出來)echo $class_stu['甲班'][2]
會印出「王同學」
$class_stu['甲班'] = [1 => '吳同學', 2 => '王同學', 4 => '林同學'];
$class_stu['乙班'] = [1 => '張同學', '陳同學', '梁同學', '郭同學'];
$class_stu['丙班'] = [1 => '施同學', '黃同學', '楊同學', '蔡同學'];
echo $class_stu['乙班'][2]
會印出「陳同學」echo $class_stu['丙班'][2]
會印出「黃同學」範例:
<?php
$stu = [1 => '吳同學', 2 => '王同學', 4 => '林同學'];
$class['甲班'] = $stu;
$class['乙班'] = [1 => '張同學', '陳同學', '梁同學', '郭同學'];
$class['丙班'] = [1 => '施同學', '黃同學', '楊同學', '蔡同學'];
$year['一年級'] = $class;
$year['二年級'] = $class;
foreach ($year as $year_name => $all_class) {
echo "<h2>{$year_name}</h2>";
foreach ($all_class as $class_name => $students) {
echo "<h3>{$class_name}</h3>";
foreach ($students as $num => $name) {
echo "{$name} ({$num})<br>";
}
}
}
// dd($year);
// echo $class_stu['丙班'][1];
// 顯示變數內容
function dd($array = [])
{
header("Content-Type: application/json; charset=utf-8");
die(json_encode($array, 256));
}