PHP数组

 

 

 定义一个数组

1. <?php
$a1=array("red","green");
$a2=array("blue","yellow");
print_r(array_merge($a1,$a2));
?>

2. $arr=array($arr as $key=>$value)

 

     

  字符串转换为数组: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....设置了样式
效果

 


 
posted @ 2017-05-24 14:46  yaomengli  阅读(139)  评论(0编辑  收藏  举报