类型 | 描述 |
---|---|
数组 Array |
数组可以在一个变量中存储多个值 |
$arr = array();
var_dump( $arr );
$arrs = [];
var_dump( $arrs );
$arr = array(
'欧阳',
'西门',
'灭绝'
);
var_dump( $arr );
$arr = [
'ouyang' => '欧阳',
'ximen' => '西门',
'miejue' => '灭绝'
];
var_dump( $arr );
$arr = [
'欧阳',
'西门',
'灭绝'
];
echo $arr[0];
echo '<hr>';
echo $arr[1];
echo '<hr>';
echo $arr[2];
echo '<hr>';
$arrs = [
'ouyang' => '欧阳',
'ximen' => '西门',
'miejue' => '灭绝'
];
echo $arrs['ouyangke'];
echo '<hr>';
echo $arrs['huangrong'];
echo '<hr>';
echo $arrs['guojing'];
echo '<hr>';
$arr = [
'欧阳',
'西门',
'灭绝'
];
print_r($arr);
$var1 = 'PHP讲师';
$var2 = '欧阳';
var_dump( $var1 . $var2 );
var_dump( $var1 . ':' . $var2 );
$arr = array(
array(
'name' => '欧阳',
'school' => 'PHP中文网'
),
array(
'name' => '西门',
'school' => 'PHP中文网'
),
array(
'name' => '灭绝',
'school' => 'PHP中文网'
)
);
var_dump($arr);
print_r($arr);
$arr = [
[
'name' => '欧阳',
'school' => 'PHP中文网',
'gongfu' => [
'PHP',
'小程序',
'layui',
'Thinkphp'
]
],
[
'name' => '西门',
'school' => 'PHP中文网',
'gongfu' => [
'PHP',
'Thinkphp',
'Laravel',
'实战项目'
]
],
[
'name' => '灭绝',
'school' => 'PHP中文网',
'gongfu' => [
'HTML',
'PHP',
'layui',
'Thinkphp'
]
]
];
var_dump($arr);
print_r($arr);
备:数组最好不要超过 3 层
$arr = [
[
'name' => '欧阳',
'school' => 'PHP中文网',
'gongfu' => [
'PHP',
'小程序',
'layui',
'Thinkphp'
]
],
[
'name' => '西门',
'school' => 'PHP中文网',
'gongfu' => [
'PHP',
'Thinkphp',
'Laravel',
'实战项目'
]
],
[
'name' => '灭绝',
'school' => 'PHP中文网',
'gongfu' => [
'HTML',
'PHP',
'layui',
'Thinkphp'
]
]
];
echo $arr[0]['name'].' --- ';
echo $arr[0]['gongfu'][0].' --- ';
echo $arr[0]['gongfu'][1];
备:数组访问时,层次不要弄错
$arr = array(
'ouyang' => '欧阳',
'ximen' => '西门',
'miejue' => '灭绝'
);
foreach( $arr as $v ){
echo $v;
echo '<hr>';
}
$k
和$v
变量名,可以自定义
示例1:
$arr = array[
'ouyang' => '欧阳',
'ximen' => '西门',
'miejue' => '灭绝'
];
foreach( $arr as $k=>$v ){
echo $k . ' --- ' . $v;
echo '<hr>';
}
示例2:
$arr = [
'欧阳',
'西门',
'灭绝'
];
foreach( $arr as $k=>$v ){
echo $k . ' --- ' . $v;
echo '<hr>';
}
示例1:
$arr = [
[
'name' => '欧阳',
'school' => 'PHP中文网'
],
[
'name' => '西门',
'school' => 'PHP中文网'
],
[
'name' => '灭绝',
'school' => 'PHP中文网'
]
];
foreach( $arr as $k=>$v ){
print_r($v);
echo '<hr>';
}
示例2:
$arr = [
[
'name' => '欧阳',
'school' => 'PHP中文网'
],
[
'name' => '西门',
'school' => 'PHP中文网'
],
[
'name' => '灭绝',
'school' => 'PHP中文网'
]
];
foreach( $arr as $k=>$v ){
foreach ($v as $key => $value) {
echo $value;
echo '<hr>';
}
}
示例3:
$arr = [
[
'name' => '欧阳',
'school' => 'PHP中文网'
],
[
'name' => '西门',
'school' => 'PHP中文网'
],
[
'name' => '灭绝',
'school' => 'PHP中文网'
]
];
foreach( $arr as $k=>$v ){
echo $v['name'].' --- '.$v['school'];
echo '<hr>';
}