Blogger Information
Blog 29
fans 0
comment 0
visits 27000
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP基础:数组函数之分割与合并、检查与获取、排序与差集、取出与移除
暴宇
Original
656 people have browsed it

PHP基础:数组函数

1.数组函数之数组的分割与合并

  1. // (1)array_chunk 将一个数组分割成多个
  2. $arr=array('id'=>'1','name'=>'baoyu','age'=>30,'sex'=>'man');
  3. echo '将一个数组分割为多个数组,每个数组包含2个键值对<br>';
  4. printf('返回索引数组<pre>%s</pre>',print_r(array_chunk($arr,2),true));
  5. printf('返回关联数组<pre>%s</pre>',print_r(array_chunk($arr,2,true),true));
  6. echo '<hr>';
  7. // (2)array_merge 合并一个或多个数组
  8. // 如存在相同键名,则后面数组的值会覆盖前面数组的值,索引键名不覆盖
  9. $arr1=['id'=>'101',1,2,3];
  10. $arr2=['name'=>'baoyu',4,5,'a'];
  11. $arr3=['id'=>'103',7,8,'b'];
  12. $arrs=array_merge($arr1,$arr2,$arr3);
  13. printf('合并后的数组:<pre>%s</pre>',print_r($arrs,true));
  14. echo '<hr>';


2.数组函数之检查与获取

  1. // (3)array_column 返回数组中指定的一列
  2. $arr=[['id'=>'101','name'=>'baoyu','age'=>30,'sex'=>'man'],
  3. ['id'=>'102','name'=>'zhangyi','age'=>32,'sex'=>'woman'],
  4. ['id'=>'103','name'=>'lishan','age'=>31,'sex'=>'man']];
  5. printf('返回数组中的name列的值,键名为id<pre>%s</pre>',print_r(array_column($arr,'name','id'),true));
  6. echo '<hr>';
  7. // (4)array_key_exists 检查数组里是否有指定的键名或索引
  8. $arr=['id'=>'101','name'=>'baoyu','age'=>30,'sex'=>'man'];
  9. if(array_key_exists('name',$arr)){
  10. echo '数组中包含name这个键名<br>';
  11. };
  12. // (5)array_key_first 获取数组的第一个键名
  13. echo '返回数组中的第一个键名是:',array_key_first($arr),'<br>';
  14. // (6)array_key_last 获取数组的最后一个键名
  15. echo '返回数组中的最后一个键名是:',array_key_last($arr),'<br>';
  16. // (7)array_keys 返回数组中部分的或所有的键名
  17. printf('返回数组中所有的键名:%s',print_r(array_keys($arr),true));
  18. echo '<hr>';


3.数组函数之排序

  1. // (8)array_multisort 对多个数组或多维数组进行排序
  2. $arrs=['number'=>[101,20,30,50],
  3. 10=>[102,'name'=>'zhangyi',32,'sex'=>'woman'],
  4. 'string'=>['103','name'=>'lishan','31','sex'=>'man']];
  5. $arr1=[6,3,8];
  6. array_multisort($arrs['number'],SORT_NUMERIC,SORT_ASC,$arrs[10],SORT_STRING,SORT_DESC,$arrs['string'],SORT_NATURAL,SORT_ASC);
  7. printf('数组多维排序:<pre>%s</pre>',print_r($arrs,true));
  8. array_multisort($arrs,SORT_FLAG_CASE);//
  9. printf('数组单维排序:<pre>%s</pre>',print_r($arrs,true));
  10. echo '<hr>';


3.数组函数之差集

  1. // (9)array_diff 计算数组的差集
  2. $arr1=[1,2,3,4];
  3. $arr2=[3,4,5,6];
  4. $arr3=[5,6,7,8];
  5. $arrs=array_diff($arr1,$arr2,$arr3);
  6. printf('返回第1个数组与其他数组的值差集:<pre>%s</pre>',print_r($arrs,true));
  7. echo '<hr>';
  8. // (10)array_diff_key 使用键名比较计算数组的差集
  9. $arr1=['id'=>'101','name'=>'baoyu','age'=>30,'sex'=>'man'];
  10. $arr2=['name'=>'baoyu','job'=>'admin'];
  11. $arr3=['id'=>'101','tel'=>'18912345678'];
  12. $arrs=array_diff_key($arr1,$arr2,$arr3);
  13. printf('返回第1个数组与其他数组的键差集:<pre>%s</pre>',print_r($arrs,true));

3.数组函数之取出与移除

  1. // (11)array_slice 从数组中取出一段
  2. $arr=[1,2,3,4,5,6,7,8,9,10];
  3. $outarr=array_slice($arr,2,4);
  4. printf('从第3个位置开始取出4个元素:<pre>%s</pre>',print_r($outarr,true));
  5. $outarr=array_slice($arr,-3);
  6. printf('取出从倒数第3至最后的元素:<pre>%s</pre>',print_r($outarr,true));
  7. $outarr=array_slice($arr,-6,-4);
  8. printf('取出从倒数第6至倒数第4之间的元素:<pre>%s</pre>',print_r($outarr,true));
  9. // (12)array_splice 去掉数组中的某一部分并用其它值取代
  10. $arr=[1,2,3,4,5,6,7,8,9,10];
  11. $outarr=array_splice($arr,2,4,[101,102,103,104]);
  12. printf('从第3个位置开始被移除的4个元素:<pre>%s</pre>',print_r($outarr,true));
  13. printf('被替换后的数组:<pre>%s</pre>',print_r($arr,true));
  14. $outarr=array_splice($arr,-3,-2,'201');
  15. printf('将倒数第3个元素替换为201:<pre>%s</pre>',print_r($arr,true));
  16. $outarr=array_splice($arr,-5,-2);
  17. printf('移除从倒数第5至倒数第2之间的元素:<pre>%s</pre>',print_r($arr,true));



Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:这是在补作业吗? 加油
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!