1. 基本形式定义数组 $array=array();
print_r可以打印复杂数据类型 函数
2. 定义数组$array1 = array(‘李四‘,‘宁‘,‘凌‘);
3. 向数组中添加函数(需要添加元素的数组,元素1,元素2) :array_push($array1,‘二狗‘,‘狗蛋‘);
4. range(开始,结束,间隔) 返回值数组
$array3 = range(1,10,2);
echo ‘<pre>‘; //原样输出
print_r($array3);
echo ‘</pre>‘;
//Array( [0] => 1 [1] => 3 [2] => 5 [3] => 7 [4] => 9)
5.键值对形式
$array5 = array();
$array5[‘zhang‘] = ‘zhangsan‘;
$array5[‘wang‘] = ‘wangbadan‘;
$array5[‘li‘] = ‘lisi‘;
$array5[‘tang‘] = ‘tangdasha‘;
echo ‘<pre>‘;
print_r($array5);
echo ‘</pre>‘;
//Array( [zhang] => zhangsan [wang] => wangbadan [li] => lisi [tang] => tangdasha)
6.遍历上面的数组foreach
foreach ($array5 as $key=>$value){
echo $key . ‘ : ‘. $value . ‘<br>‘;
}
//zhang:zhangsan
wang:wangbadan
li:lisi
tang:tangdasha
注:键的名字要不一样,要不后面的会覆盖前面的
7.数组长度:count($array6);
8.删除数组中的元素
$array7 = array(1,2,3,4);
array_splice($array7,1,1); //array_splice(数组,index,length);
9.二维数组
$array9 = array(array(‘秋衣‘,‘秋裤‘),array(‘棉衣‘,‘棉裤‘,‘棉鞋‘));
echo ‘<pre>‘;
print_r($array9);
echo ‘</pre>‘;
//Array( [0] => Array ( [0] => 秋衣 [1] => 秋裤 ) [1] => Array ( [0] => 棉衣 [1] => 棉裤 [2] => 棉鞋 ))
10.键值对形式二维数组
$array8 = array(‘秋‘=>array(‘秋衣‘,‘秋裤‘),‘棉‘=>array(‘棉衣‘,‘棉裤‘,‘棉裤‘));
echo ‘<pre>‘;
print_r($array8);
echo ‘</pre>‘;
//Array( [秋] => Array ( [0] => 秋衣 [1] => 秋裤 ) [棉] => Array ( [0] => 棉衣 [1] => 棉裤 [2] => 棉裤 ))
11.遍历二维数组
遍历array9
$array9 = array(array(‘秋衣‘,‘秋裤‘),array(‘棉衣‘,‘棉裤‘,‘棉鞋‘));
for($i= 0;$i < count($array9);$i++){
for($k = 0;$k < count($array9[$i]);$k++){
echo $array9[$i][$k].‘---‘;
}
echo ‘<br>‘;
} //秋衣---秋裤---
棉衣---棉裤---棉鞋---