一、创建数组
代码如下:
$arr = array('php','mysql','database');
var_dump($arr);
echo '<hr>';
$arr = [
'php','mysql','database'
];
var_dump($arr);
echo '<hr>';
测试结果:
二、创建关联数组、索引数组
关联数组:
$arr = [
'name' => 'hhappysong',
'age' => '45',
'salary' => '20000'
];
var_dump($arr);
echo '<hr>';
索引数组:
$arr = [
'php','mysql','database'
];
var_dump($arr);
echo '<hr>';
测试结果:
三、访问数组数据
代码如下
$arr = [
'php','mysql','database'
];
var_dump($arr[0]);
echo '<hr>';
var_dump($arr[2]);
echo '<hr>';
$arr = [
'name' => 'hhappysong',
'age' => '45',
'salary' => '20000'
];
var_dump($arr['name']);
echo '<hr>';
var_dump($arr['salary']);
echo '<hr>';
测试结果:
四、创建二维数组、三维数组
代码如下:
<?php
$arr = [
['xiaoming','18','php'],
['hong','18','mysql'] ,
['liming','20','html']
];
var_dump($arr);
echo '<hr>';
$arr = [
['xiaoming','18','php',[
'football','bingpang','badmitton'
]
],
['hong','18','mysql',[
'football','bingpang','badmitton'
]
],
['liming','20','html',[
'football','bingpang','badmitton'
]
]
];
var_dump($arr);
echo '<hr>';
测试结果:
手抄代码如下:
五、遍历二维数组、三维数组
遍历二维数组:
代码如下: <?php
$arr = [
['xiaoming','18','php'],
['hong','18','mysql'] ,
['liming','20','html']
];
foreach ($arr as $v){
foreach ($v as $vv){
echo $vv;
echo '<hr>';
}
}
测试结果:
遍历三维数组:
代码如下:
<?php
$arr = [
['xiaoming','18','php',[
'football','bingpang','badmitton'
]
],
['hong','18','mysql',[
'football','bingpang','badmitton'
]
],
['liming','20','html',[
'football','bingpang','badmitton'
]
]
];
foreach($arr as $a){
foreach ($a as $aa){
foreach ($aa as $aaa){
echo $aaa;
echo '<hr>';
}
}
};
echo '<hr>';
测试结果:
手抄代码如下: