PHP数组
定义一个数组
1. <?php
$a1=array("red","green");
$a2=array("blue","yellow");
print_r(array_merge($a1,$a2)
);
?>
2. $arr=array($arr as $key=>$value)
![](https://images2015.cnblogs.com/blog/1066696/201705/1066696-20170524150927904-1478476571.png)
字符串转换为数组:implode()
数组转换为字符串:explode("从哪里开始分割",数组名) 例如 explode(",",$arr)
像数组中添加或删除元素 (跟js中一样 (push(末尾添加) pop(末尾删除) unshift (像开头添加元素)shift(删除开头元素))
遍历数组 foreach(数组名 as $key=>$value)
数组与字符串综合使用
<?php
header("Content-type:text/html; charset=utf-8");
$arr=array("blue","red","yellow","green","pup","pop");
$string="勇敢,可爱,低调,淘气,直率,霸气";
$arr_1=explode(",", $string); //将字符串转换为数组
foreach ($arr_1 as $key=> $value){ //遍历数组
$index=array_rand($arr,1); //随机获取数组里面的一个键名
// echo "<span class="."$arr[$index]".">$value</span>";
echo "<span class=".$arr[$index].">$value</span>";
}
?>
给 span标签 .blue .red .yeelow....设置了样式
效果