编程 : 数组与字符串,变量之间的转换.
实例
<?php //list list($name,$couse,$garde) = ['八神','dede',80]; echo $name,'的',$couse,'课程成绩是',$garde,'<hr>'; //extract($arr,$flag) $arr1 = ['id'=>1, 'name'=>'杨过','sex'=>'male','salary'=>8000]; extract($arr1); echo '我的id:',$id,',姓名:',$name,',性别:',$sex,',工资: ',$salary,'元<hr>'; //compact $name = '陈近南'; $faction = '天地会'; $position = '总舵主'; $arr = compact('name','faction','position'); echo var_export($arr,true),'<hr>'; echo '<pre>'; //explode $lang = 'html,css,javascript,jquery,php,mysql'; echo var_export(explode(',',$lang)),'<br>'; echo var_export(explode(',',$lang,3)),'<br>'; echo var_export(explode(',',$lang,-2)),'<br>';//最常用 //implode $arr = ['首页','公司新闻','公司新闻','联系我们']; echo var_export(implode($arr),true),'<br>'; echo var_export(implode('|',$arr),true),'<br>'; echo var_export('<a href="#">'.implode('</a>|<a href="#">',$arr).'</a>'),'<br>';
运行实例 »
点击 "运行实例" 按钮查看在线实例
编程: 数组元素的回调处理
实例
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/8/23 * Time: 13:25 */ $arr1 = [5,0,'',20,null,88,false,'php']; echo '<pre>'; echo '原始数组',var_export($arr1,true),',共有:',count($arr1),'个元素<hr>'; $arr2 = array_filter($arr1); echo '新数组',var_export($arr2,true),',共有:',count($arr2),'个元素<hr>'; $arr3 = ['html','css','javascript']; $arr4 =array_filter($arr3, function ($value){ return $value !== 'css'; }); echo var_export($arr4),'<hr>'; $arr = ['name'=>'admin','email'=>'admin@php.cn']; echo var_export($arr, true), '<hr>'; //格式化 array_walk($arr, function (&$value, $key) { echo $key,':',$value,'<br>'; }); echo '<hr>'; //array_walk($arr, function (&$value, $key, $name) { // //如果当前的用户名是:admin,则授权查看,否则拒绝 // if ($value != $name) { // exit('无权查看'); // } else { // exit($key.':'.$value); // } // //},'admin'); //echo '<hr>'; //echo '<pre>'; $arr = ['东邪','西毒','南帝','北丐']; shuffle($arr); // 验证码 echo var_export($arr); //array_merge():合并多个数组,相同键名互相覆盖 $db_sys = ['host'=>'127.0.0.1','user'=>'root','pass'=>'root']; $db_user = ['host'=>'localhost', 'pass'=>sha1('123456')]; $res=array_merge($db_sys, $db_user); echo var_export($res);
运行实例 »
点击 "运行实例" 按钮查看在线实例
编程: 用for()循环来遍历
实例
<?php /** * 1.常用的键值操作 * 2.数组内部指针操作(巡航) */ $arts = ['亢龙有悔','飞龙在天','见龙在田','鸿渐于陆','潜龙勿用','突如其来']; $res1 = ''; for ($i=0; $i<count($arts); $i++) { $res1 .= $arts[$i].'---'; } echo rtrim($res1,'---'),'<hr>';
运行实例 »
点击 "运行实例" 按钮查看在线实例