Blogger Information
Blog 26
fans 0
comment 0
visits 15648
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
数组函数的练习
庄周梦蝶
Original
632 people have browsed it
  1. <?php
  2. //1.把一个数值拆分成多个二维数组
  3. $acd=[1,2,3,4,5,6,7];
  4. echo '<pre>'.print_r(array_chunk($acd,2),true).'</pre>';
  5. echo '<pre>'.print_r(array_chunk($acd,2,true),true).'</pre>';

  1. <?php
  2. //2.合并两个数组,其中一个当健名,一个当值
  3. $a=['id','name','email','age'];
  4. $b=['1','小马','253166555@qq.com','20'];
  5. $c=array_combine($a,$b);
  6. print_r($c);

  1. <?php
  2. //3.看看数组的内容并且都有几个
  3. $a=[1,1,'hello',5,5,'width','haoayou'];
  4. print_r(array_count_values($a));

  1. <?php
  2. $a=[1,2,3,4,5];
  3. $b=[1,2,6,4,5,7];
  4. //4.返回第一个数组不同于第二个数组的值
  5. $c=array_diff($a,$b);
  6. print_r($c);

  1. <?php
  2. //5.填充个数组,参数是从几键来填充,填充几个,内容是什么
  3. $a=array_fill(0,5,'abc');
  4. $b=array_fill(-2,4,'啊啊');
  5. echo print_r($a,true),'<br>';
  6. echo print_r($b,true),'<br>';
  7. //用数组的值来当键填充内容,非法标识符自动改为字符串
  8. $c=['for',4,'你好',10];
  9. $d=array_fill_keys($c,'aaa');
  10. echo print_r($d,true),'<br>';

  1. <?php
  2. //6.把数组中的每个单元过一遍函数,结果为真的返回,假的省略,健名不变
  3. function odd($abc)
  4. {
  5. return($abc%2);
  6. }
  7. function dod($abc)
  8. {
  9. return(!($abc%2));
  10. }
  11. $a=[1,2,3,4,5,6,7,8,9,10];
  12. echo print_r(array_filter($a,'odd'),true),'<br>';
  13. echo print_r(array_filter($a,'dod'),true),'<br>';

  1. <?php
  2. //7.两个数组合并,键从新开始
  3. $a=['a'=>2,1,3,4];
  4. $b=['a','b','a'=>9,10,11];
  5. $c=array_merge($a,$b);
  6. //但是合并的时候相同的健名的值会顶替
  7. print_r($c);
  8. echo '<br>';
  9. //用这个,相同健名的值会合并成一个二维数组来显示
  10. $c=array_merge_recursive($a,$b);
  11. echo '<pre>'.print_r($c,true).'</pre>';

  1. <?php
  2. //8.数组里的值从小到大进行排序
  3. $a1=[1,5,3,8,9];
  4. array_multisort($a1);
  5. var_dump($a1);
  6. echo '<hr>';
  7. //排序二维数组
  8. $a1=[
  9. [1,5,4,3,7,10],
  10. [11,20,45,30,15,25]
  11. ];
  12. array_multisort($a1[0]);
  13. //降序排列
  14. array_multisort($a1[1],SORT_DESC);
  15. echo '<pre>'.print_r($a1,true).'</pre>';

  1. <?php
  2. //9.随机从数组里面抽取一个或多个值
  3. $arr=range(1,100);
  4. echo print_r(array_rand($arr,2),true);

  1. <?php
  2. //10.用一个数值替换掉另外一个数值内相同健名的值
  3. $a1=['name'=>'小马','email'=>'222@qq.com','gz'=>888,'age'=>19];
  4. $a2=['gz'=>9999];
  5. $a3=['age'=>20];
  6. echo "{$a1['name']}的去年工资是:{$a1['gz']}元,年龄是{$a1['age']}<br>";
  7. $a1=array_replace($a1,$a2,$a3);
  8. echo "{$a1['name']}的今年工资是:{$a1['gz']}元,年龄是{$a1['age']}";

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