今天学习了常用数组函数的用法:
代码:
实例
<?php list($a,$b,$c)=array('apple','banbana','orange'); echo $a,',',$b,',',$c,'都是水果'; echo '<hr>'; $arr1=['id'=>1,'name'=>'tom','age'=>18,'sex'=>'male']; echo '共生成了:',var_export(extract($arr1,true)),'个变量:<br>'; echo '我的id:',$id,',姓名:',$name,'年龄:',$age,'性别:',$sex; echo '<hr>'; $id=2; $name='jack'; $age=20; $arr2=compact('id','name','age'); echo var_export($arr2,true); echo '<hr>'; $lang='php,html,css,javascript,jquery,thinkphp,mysql'; echo var_export(explode(',',$lang)),'<br>'; echo var_export(explode(',',$lang,3)),'<br>'; echo var_export(explode(',',$lang,-2)),'<br>'; echo '<hr>'; $arr3=[3,'jim',18,'male']; echo var_export(implode($arr3),true),'<br>'; echo var_export(implode('|',$arr3),true),'<br>'; echo var_export('<a href="#">'.implode('</a>|<a href="#">',$arr3).'</a>');
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例
<?php list($a,$b,$c)=array('apple','banbana','orange'); echo $a,',',$b,',',$c,'都是水果'; echo '<hr>'; $arr1=['id'=>1,'name'=>'tom','age'=>18,'sex'=>'male']; echo '共生成了:',var_export(extract($arr1,true)),'个变量:<br>'; echo '我的id:',$id,',姓名:',$name,'年龄:',$age,'性别:',$sex; echo '<hr>'; $id=2; $name='jack'; $age=20; $arr2=compact('id','name','age'); echo var_export($arr2,true); echo '<hr>'; $lang='php,html,css,javascript,jquery,thinkphp,mysql'; echo var_export(explode(',',$lang)),'<br>'; echo var_export(explode(',',$lang,3)),'<br>'; echo var_export(explode(',',$lang,-2)),'<br>'; echo '<hr>'; $arr3=[3,'jim',18,'male']; echo var_export(implode($arr3),true),'<br>'; echo var_export(implode('|',$arr3),true),'<br>'; echo var_export('<a href="#">'.implode('</a>|<a href="#">',$arr3).'</a>');
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例
<?php $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>'; array_walk($arr1,function (&$value,$key){ echo $key,':',$value,'<br>'; }); echo '<hr>';
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例
<?php $arr=['id'=>1,'name'=>'jim','age'=>18]; $sum=count($arr); for($i=0;$i<$sum;$i++){ echo key($arr),'---',current($arr),'<br>'; next($arr); }
运行实例 »
点击 "运行实例" 按钮查看在线实例