实例
<meta charset="UTF-8"> <?php echo "数组和变量之间的转换"; echo "<hr>"; //数组转成字符串 list($name,$course,$age)=['小龙女','php',25]; $name = '小龙女'; $course = 'php'; $age = 25; echo $name,"今年",$age,"要学习",$course; echo "<hr>"; //关联数组转为变量 $arr1=['id'=>5,'name'=>'sun','sex'=>'male','age'=>25]; var_export(extract($arr1)); //extract返回数组的数量 $id =5;$name="sun";$sex="male";$age=25; echo "我的姓名",$name,"今年",$age,"id是",$id; echo "<hr>"; // compact 将变量转为数组 $name = '陈近南'; $faction = '天地会'; $position = '总舵主'; var_export(compact("name","faction","position")); echo "<hr>"; //将字符串转为数组 explode() $lang = 'html,css,javascript,jquery,php,mysql'; echo var_export(explode(',',$lang)),"<br>"; echo var_export(explode(",",$lang,3)),"<br>"; //3个数组 echo var_export(explode(",",$lang,-2)); //-2删除二个数组 echo "<hr>"; // 导航链接的制作 $arr = ['首页','公司新闻','公司新闻','联系我们']; echo var_export(implode("|",$arr),true); echo var_export('<a href="#">'.implode('</a>|<a href="#">',$arr).'</a>');
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例
<meta charset="UTF-8"> <?php echo "数组的遍历","<br>"; $arts = ['亢龙有悔','飞龙在天','见龙在田','鸿渐于陆','潜龙勿用','突如其来']; $swordsman = []; $swordsman['name'] = '杨康'; $swordsman['position'] = '金国小王爷'; $swordsman['skill'] = '九阴白骨爪'; // print_r($arts); // echo "<hr>"; // $res1 = ''; // for ($i=0; $i<count($arts); $i++) { // $res1 .= $arts[$i].'---'; // } // echo $res1; // echo rtrim($res1,'---'); //清除右侧横线 // $value 叫循环变量,每一次数组将要输出的当前的元素赋值给$value // as 关键词 foreach($swordsman as $key=>$value){ echo $key,'=>',$value,'<br>'; }
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例
<meta charset="UTF-8"> <?php //数组的回调函数 // array_filter(); // array_walk(); // $arr1=[10,null,50,false,"php",'']; // echo "<pre>"; // echo '原始数据',var_export($arr1)."一共".count($arr1); // // null,"",false,0 返回false 其他为true // $arr2=array_filter($arr1); // echo "<hr>"; // echo '现在数据',var_export($arr2),'一共',count($arr2); // $arr3=['css','html','javascript']; // $arr4 = array_filter($arr3,function ($value){ // return $value !== 'css'; //删除css // }); // echo var_export($arr4),'<hr>'; echo "<hr>"; // array_walk $arr5 = ['name'=>'admin','email'=>'admin@php.cn']; $arr6 =array_walk($arr5,function ($value,$key,$name){ // echo $value.":".$key,"<br>"; // 如果当前的用户名是:admin,则授权查看,否则拒绝 if ($value != $name) { //exit() 函数输出一条消息,并退出当前脚本。 echo exit("无权查看"); } else{ exit($key.':'.$value); } },'admin1');
运行实例 »
点击 "运行实例" 按钮查看在线实例