菜鸟教程:(数组)https://www.runoob.com/php/php-arrays.html
? ? ? ? ? ? ? ? ? (多维数组)https://www.runoob.com/php/php-arrays-multi.html
? ? ? ? ? ? ? ? ? (数组方法)https://www.runoob.com/php/php-ref-array.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<?php
// 1、索引/数值数组 : 用for循环通过索引进行遍历,也可以用foreach遍历
$arr = array('张三', '李四', '王五');
for ($i = 0; $i < count($arr); $i++) {
echo $arr[$i], '<br/>';
}
echo '<hr/>';
foreach ($arr as $key => $value) {
echo $key, ' ', $value, '<br/>';
}
// 2、关联数组:带有指定的键的数组,每个键关联一个值,关联数组只能用foreach遍历
echo '<hr/>';
$city = array('兰州' => '甘肃', "西安" => "陕西", "银川" => "宁夏");
foreach ($city as $key => $value) {
echo $key . ' 是 ' . $value . ' 的省会城市', '<br/>';
}
echo '<hr/>';
// 3、多维数组
$sties = array( // 二维数组
array(1, 3, 5, 7, 9),
array(2, 4, 6, 8, 10),
array('a', 'b', 'c', 'd', 'e')
);
for ($i = 0; $i < count($sties); $i++) {
print_r($sties[$i]);
echo '<br/>';
$st1 = $sties[$i];
for ($j = 0; $j < count($st1); $j++) {
echo $st1[$j], '<br/>';
};
echo '<hr/>';
};
$people = array( // 多维数组
'曹魏' => array(
'昂', '丕', '植', '熊', '璋', '冲'
),
'蜀汉' => array(
'封', '禅', '永', '理'
),
'孙吴' => array(
'登', '亮'
)
);
echo '<pre>'; // 格式化输出
print_r($people);
echo '</pre>';
echo '<hr/>';
foreach ($people as $regime => $name) {
print_r($people[$regime]);
echo '<br/>';
for ($i = 0; $i < count($people[$regime]); $i++) {
echo $people[$regime][$i], '<br/>';
};
echo '<hr/>';
}
?>
</body>
</html>
|