数组的赋值: PHP中的数组既可以做数组,也可以做键值对字典,且不存在限制,非常灵活.
// 定义纯数组格式 | |
$array_one[0] = 100; | |
$array_one[1] = 200; | |
$array_one[2] = "lyshark"; | |
$array_one[3] = [1,2,3,4,5,6]; | |
echo $array_one[0] . $array_one[2] . "<br>"; | |
echo "数组中的数组: " . $array_one[3][1] . "<br>"; | |
// 定义字典格式的数组 | |
$array_two['ID'] = 1001; | |
$array_two['name'] = "lyshark"; | |
$array_two['age'] = 25; | |
echo "姓名: " . $array_two['name'] . "<br>"; | |
数组的定义与遍历: 分别定义一维数组与二维数组,并通过两种方式分别遍历数组元素.
// 定义一维数组 | |
$var = array("first"=>1,"second"=>2,"third"=>3,"montd"=>4); | |
foreach ($var as $key => $value) | |
{ | |
echo "键 => {$key} --> 值 => {$value} <br>"; | |
} | |
// 定义二维数组 | |
$var_two = array( | |
"书籍" => array("文学","历史","地理"), | |
"水果" => array(1=>"苹果",2=>"橙子",3=>"水蜜桃")); | |
foreach($var_two as $key => $value) | |
{ | |
echo "键=" . $key . " "; | |
foreach($var_two[$key] as $index => $value) | |
{ | |
echo " 索引: " . $index . " --> 值: " . $value; | |
} | |
echo "<br>"; | |
} | |
// 二维数组其他遍历方法 | |
$array_two = array( | |
array('北京','上海','深圳','广州'), | |
array('黑龙江','吉林','辽宁','江苏')); | |
for($x=0; $x< count($array_two); $x++) | |
{ | |
for($y=0; $y<count($array_two[$x]); $y++) | |
{ | |
echo "输出数组: " . $array_two[$x][$y] . "<br>"; | |
} | |
} | |
数组元素统计: 统计出数组中指定元素的个数,或者统计每个元素出现的次数.
// 一维数组统计 | |
$lamp = array("Linux","Apache","MySQL","PHP"); | |
echo "lamp 元素个数: " . count($lamp) . "<br>"; | |
// 二维数组统计 | |
$web = array( | |
'lamp' => array("Linux","Apache","MySQL","PHP"), | |
'j2ee' => array("Unix","Tomcat","Oracle")); | |
echo "二维中一维个数: " . count($web) . "<br>"; | |
echo "整个数组的大小: " . count($web,1) . "<br>"; | |
// 统计元素出现次数,返回数组 | |
$array = array("php","asp","jsp","php","python","node.js"); | |
$new_array = array_count_values($array); | |
foreach($new_array as $key => $value) | |
{ | |
echo "数组元素 => {$key} ---> 出现频率 => {$value} <br>"; | |
} | |
二维数组遍历回显: 通过传统的循环结构遍历特定数组中的元素,并用表格展示出来.
$data = array( | |
array(1,'admin','北京',"admin@blib.cn"), | |
array(2,'guest','天津',"guest@blib.cn"), | |
array(3,'lyshark','洛杉矶',"lyshark@blib.cn")); | |
echo "<table border='1' width='600' align='center'>"; | |
echo "<caption><h1>输出列表</h1></caption>"; | |
echo "<tr bgcolor='#ddddddd'>"; | |
echo "<th>编号</th><th>姓名</th><th>住址</th><th>邮箱</th>"; | |
echo "</tr>"; | |
for($row=0; $row < count($data); $row++) | |
{ | |
echo "<tr>"; | |
for($clo=0; $clo<count($data[$row]); $clo++) | |
{ | |
echo "<td>" . $data[$row][$clo] . "</td>"; | |
} | |
echo "</tr>"; | |
} | |
echo "</table>"; | |
三维数组遍历回显: 由于FOR语句遍历数组的局限性,所以PHP中提供了更加强大的ForEach结构.
$data = array( | |
"市场部" => array(array(1,"张某","经理",5000),array(2,"刘某","员工",3000)), | |
"销售部" => array(array(1,"李某","职员",2000),array(2,"孙某","经理",6000)), | |
"财务部" => array(array(1,"吴某","经理",8000)) | |
); | |
foreach($data as $sector => $table) | |
{ | |
echo "<table border='1' width='600' align='center'>"; | |
echo "<caption><h1>" . $sector . "</h1></caption>"; | |
echo "<tr bgcolor='#ddddddd'>"; | |
echo "<th>编号</th><th>姓名</th><th>职务</th><th>薪资</th>"; | |
echo "</tr>"; | |
foreach($table as $row) | |
{ | |
echo "<tr>"; | |
foreach($row as $col) | |
{ | |
echo "<td>" . $col . "</td>"; | |
} | |
echo "</tr>"; | |
} | |
echo "</table><br>"; | |
} | |
指针操作数组: 数组内部指针式数组内部的组织机制,指向一个数组中的某个元素.
$info = array('admin','guest','lyshark'); | |
// 将数组中所有的的元素转换为变量 | |
list($x,$y,$z) = $info; | |
echo "变量: " . $x . $y . $z . "<br>"; | |
// 将数组中指定元素转换为变量 | |
list(,,$z)= $info; | |
echo "变量: " . $z . "<br>"; | |
// 数组指针操作 | |
reset($info); | |
echo "元素索引: " . key($info) . "元素值: " . current($info) . "<br>"; | |
next($info); | |
echo "元素索引: " . key($info) . "元素值: " . current($info) . "<br>"; | |
prev($info); | |
echo "元素索引: " . key($info) . "元素值: " . current($info) . "<br>"; | |
end($info); | |
echo "元素索引: " . key($info) . "元素值: " . current($info) . "<br>"; | |
// 通过指针循环遍历 | |
for(reset($info); current($info);next($info)) | |
{ | |
echo "数据: " . current($info) . "<br>"; | |
} | |
数组键值对操作: 数组中的每个元素都是由键值对组成的,通过元素的键访问对应的值.
$info = array("ID" => 1001,"Name" => "admin","Age" => 24 ); | |
$var1 = array("a"=>"python","b"=>"php","c"=>"C/C++","d"=>"java"); | |
$var2 = array("1"=>"a","2"=>"b","3"=>"c","4"=>"d"); | |
// 获取所有的键值 | |
print_r(array_keys($info)); | |
print_r(array_values($info)); | |
// 获取特定的键 | |
print_r(array_keys($info,"admin")); | |
// 值作为新数组的键 | |
$new = array_combine($var1, $var2); | |
print_r($new); echo"<br>"; | |
// extrace()将数组转换成标量变量 | |
extract($var1); | |
echo $a."<br>"; | |
echo $b."<br>"; | |
判断键值是否存在: 检查特定数组中是否存在某个值,即在数组中搜索给定的值.
// 函数的简单用法 | |
$info = array("Mac","Windows NT","AIX","Linux"); | |
if(in_array("Mac",$info)) | |
echo "存在 <br>"; | |
// 严格模式,必须类型一致,否则失败 | |
$info = array('10.24',12,45,1,2); | |
if(in_array(10.24,$info,true)) | |
echo "存在 <br>"; | |
// 查询数组中的数组 | |
$info = array(array(1,2),array(3,4)); | |
if(in_array(array(3,4),$info)) | |
echo "存在 <br>"; | |
// search 同样可以实现检索 | |
$info = array("ID" => 1001,"Name" => "admin","Age" => 24 ); | |
if(array_search("admin",$info)) | |
echo "存在 <br>"; | |
// 检查下标元素是否在数组中 | |
if(array_key_exists('ID',$info)) | |
echo "存在 <br>"; | |
// 使用isset同理 | |
if(isset($info['ID'])) | |
echo "存在 <br>"; | |
数组交换/统计/去重: 实现数组的交换,统计,与去重等操作.
$info = array("OS" => "Linux","WebServer" => "Nginx", "DataBase" => "MySQL"); | |
// 交换数组中的键值 | |
$new_info = array_flip($info); | |
print_r($info); echo "<br>"; | |
print_r($new_info); echo "<br>"; | |
// 数组元素反转 | |
$new_info = array_reverse($info); | |
print_r($info); echo "<br>"; | |
print_r($new_info); echo "<br>"; | |
// 统计元素个数 | |
$var_count = count($info); | |
echo "元素个数: " . $var_count . "<br>"; | |
// 统计数组中所有元素出现次数 | |
$array = array(1,2,3,3,2,4,5,6,6,7,7,7,6,8,7,4,3); | |
$array_count = array_count_values($array); | |
print_r($array_count); echo "<br>"; | |
// 数组元素去重操作 | |
$array = array("1" => "PHP","2" => "MySQL","3" => "PHP"); | |
print_r(array_unique($array)); | |
数组回调与过滤: PHP提供了回调函数,可以实现对数组中元素的过滤功能,例如将每个元素递增10等.
// array_filter 定义第一个回调函数 | |
function EvenNumber($var) | |
{ | |
if($var %2 ==0) return true; | |
} | |
$info = array(1,2,3,4,5,6,7,8,9,10); | |
print_r(array_filter($info,"EvenNumber"));echo "<br>"; | |
// array_walk 第二种回调函数的过滤写法 | |
function PrintFunction($value,$key) | |
{ | |
$value = $value + 10; | |
echo "KEY= " . $key . " VALUE= " . $value . "<br>"; | |
} | |
$info = array("UID" => 1001,"GID" => 0, "age"=> 25); | |
array_walk($info,"PrintFunction"); | |
// array_map 第三种回调函数过滤写法(可替换指定数组元素) | |
function ReplaceMap($var1) | |
{ | |
if($var1 === "MySQL") | |
return "Oracle"; | |
return $var1; | |
} | |
$info = array("Linux","Windows","Apache","MySQL","PHP"); | |
print_r(array_map("ReplaceMap",$info));echo "<br>"; | |
// array_map 传递多个参数实现回调 (可用于对比多个数组的异同点) | |
function Contrast($var1,$var2) | |
{ | |
if($var1 == $var2) | |
return "True"; | |
return "False"; | |
} | |
$Array_A = array("Linux","PHP","MySQL"); | |
$Array_B = array("Linux","MySQL","MySQL"); | |
print_r(array_map("Contrast",$Array_A,$Array_B)); | |
基本的数组排序: 在PHP中提供了多种排序函数,相比于C来说更加的简单实用.
// 对数组进行正反向排序 | |
$info = array(5,6,8,2,7,8,9,0,2,1,3,4,5); | |
if(sort($info)) | |
echo "sort 正向排序:"; print_r($info);echo "<br>"; | |
if(rsort($info)) | |
echo "sort 反向排序:"; print_r($info);echo "<br>"; | |
// 根据key=>键名称来实现数组排序 | |
$info = array(5 => "five",8 => "eight",1 => "one"); | |
if(ksort($info)) | |
echo "ksort 正向排序:"; print_r($info);echo "<br>"; | |
if(krsort($info)) | |
echo "ksort 反向排序:"; print_r($info);echo "<br>"; | |
// 根据value=>值来实现数组排序 | |
$info = array(5 => "five",8 => "eight",1 => "one"); | |
if(asort($info)) | |
echo "asort 正向排序:"; print_r($info);echo "<br>"; | |
if(arsort($info)) | |
echo "asort 反向排序:"; print_r($info);echo "<br>"; | |
// 根据自然排序法对数组排序 | |
$info = array("file1","file11","File2","FILE12","file"); | |
if(natsort($info)) | |
echo "natsort 大写排在前面:"; print_r($info);echo "<br>"; | |
if(natcasesort($info)) | |
echo "natcasesort 忽略大小写自然排序:"; print_r($info);echo "<br>"; | |
自定义/多维数组排序: 除了使用上面提供的排序方法以外,我们还可以自定义排序规则.
// 回调函数实现自定义排序 | |
$info = array("Linux","Apache","MySQL","PHP"); | |
function SortByLen($var1,$var2) | |
{ | |
// 如果两个字符串长度一致则保留 | |
if(strlen($var1) === strlen($var2)) | |
return 0; | |
else | |
return (strlen($var1)>strlen($var2)?1:-1); | |
} | |
if(usort($info,"SortByLen")) | |
echo "自定义排序:"; print_r($info);echo "<br>"; | |
// 多维数组的排序方法 | |
$info = array( | |
array("id" => 1,"soft" => "Linux","rating" => 8), | |
array("id" => 2,"soft" => "Apache","rating" => 1), | |
array("id" => 3,"soft" => "Nginx","rating" => 5) | |
); | |
foreach($info as $key => $value) | |
{ | |
// 将$info中每个元素中键为soft的值形成新数组$soft | |
$soft[$key] = $value["soft"]; | |
$rating[$key] = $value['rating']; | |
} | |
array_multisort($rating,$soft,$info); // 开始排序 | |
echo "多维数组排序: "; print_r($info); echo "<br>"; | |
拆分/合并/分解数组: 数组常用的处理函数,包括对数组进行拆分,合并,结合,等常用操作.
// array_slice(数组,开始下标,取出个数) 在数组中根据条件取值并返回. | |
$info = array("Linux","PHP","Apache","MySQL","Nginx","Zabbix"); | |
print_r(array_slice($info,1,2));echo "<br>"; | |
// array_splice(数组,开始下标,替换次数,替换为) 替换指定数组中的指定元素 | |
$info = array("Linux","PHP","Apache","MySQL","Nginx","Zabbix"); | |
array_splice($info,1,1,"AAAA"); | |
echo "将下标1的位置替换为AAAA "; print_r($info); echo "<br>"; | |
array_splice($info,2); | |
echo "从第2个元素向后的都删除掉 "; print_r($info); echo "<br>"; | |
// array_combine() 将两个数组合并为新数组 (两个数组必须相等) | |
// 其中第一个数组为KEY 第二个数组为Value | |
$key = array("1","2","3"); | |
$value = array("Apache","Nginx","Cpp"); | |
$new_array = array_combine($key,$value); | |
echo "合并后: "; print_r($new_array); echo "<br>"; | |
// array_merge() 将两个数组合并为新数组 (合并时自动去重) | |
$varA = array("a" => "Linux","b" => "MySQL"); | |
$varB = array("c" => "Python","a" => "PHP"); | |
echo "合并后: "; print_r(array_merge($varA,$varB)); echo "<br>"; | |
// array_ntersect()/array_diff() 计算两个数组的交集/差集 | |
$varA = array(1,2,3,4,5); | |
$varB = array(3,4,5,6,7); | |
echo "交集: "; print_r(array_intersect($varA,$varB));echo "<br>"; | |
echo "差集: "; print_r(array_diff($varA,$varB));echo "<br>"; | |
数组模拟实现栈/队列: 数组同样可以实现栈与队列的基本操作,如下所示.
// 实现数组栈 | |
$stack = []; | |
array_push($stack,1); | |
array_push($stack,2); | |
array_pop($stack); | |
echo "简易版: "; print_r($stack);echo "<br>"; | |
// key-value类型的栈 | |
$kv_stack = array(1 =>"Linux"); | |
array_push($kv_stack,2,"Apache"); | |
echo "key-value版栈: "; print_r($kv_stack);echo "<br>"; | |
// 实现队列 | |
$kv_loop = array(1 =>"Linux"); | |
array_unshift($kv_loop, 2, "Apache"); | |
array_shift($kv_loop); | |
echo "key-value 版队列: "; print_r($kv_loop);echo "<br>"; | |
数组的打乱/互转: 实现对数组的打乱,或将数组与字符串实现互相转换.
// $var = array(1,2,3,4,5,6,7,8,9,10); | |
$var = range(0,10); | |
echo "当前数组: "; print_r($var); echo "<br>"; | |
echo "随机取出一个元素: " . array_rand($var,1) . "<br>"; | |
echo "随机打乱数组: " . shuffle($var) . "<br>"; | |
echo "数组求和: " . array_sum($var) . "<br>"; | |
array_unshift($var, "perl"); # 在数组头部添加元素 | |
print_r($var);echo "<br>"; | |
array_push($var, "golang"); # 在数组尾部添加元素 | |
print_r($var);echo "<br>"; | |
array_shift($var); # 删除数组第一个元素 | |
print_r($var);echo "<br>"; | |
array_pop($var); # 删除数组最后一个元素 | |
print_r($var);echo "<br>"; | |
// 将数组转为字符串/数组到字符串,互转. | |
$var = array("python","php","C","java"); | |
$var = implode(",", $var); # 数组转换为字符串 | |
echo "字符串: "; print_r($var); echo "<br>"; | |
$var = explode(",", $var); # 字符串转数组 | |
echo "数组: "; print_r($var); echo "<br>"; | |