Blogger Information
Blog 34
fans 0
comment 0
visits 18331
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
0429作业
千山暮雪
Original
563 people have browsed it

1. 如何将以下二维数组里的键值name换成user ,其他保持不变? $data = [[‘name’=>’zhangdan’,’id’=>2],[‘name’=>’lisi’,’id’=>1]];

  1. $data = array_map(function ($item) {
  2. $a = array_replace(array_keys($item),[ 0=>'user']);
  3. return array_combine($a,array_values($item));
  4. }, $data);
  5. var_dump($data); //[['user'=>'zhangdan','id'=>2],['user'=>'lisi','id'=>1]]

2. 生成一个由1-100组成的数组,要求返回该数组中的偶数组成的新数组,并且新数组的索引从0开始? (提示:你可能会用到的函数:range(), array_map, array_filter, array_values)?

  1. // 通过range()生成数组
  2. $arr = range(1,100);
  3. //方法一,使用array_map
  4. $arr2 =[];
  5. array_map(function ($item) use (&$arr2) {
  6. if (($item % 2) === 0):
  7. $arr2[] = $item;
  8. endif;
  9. },$arr);
  10. echo "<pre>";
  11. print_r($arr2);
  12. //方法二
  13. // 回调直接使用到array_filter中也 可实现
  14. $arr4 = array_values(array_filter($arr, function ($item) {
  15. if (($item % 2) === 0):
  16. return $item;
  17. endif;
  18. }));
  19. echo "<pre>";
  20. print_r($arr4);
Correcting teacher:灭绝师太灭绝师太

Correction status:qualified

Teacher's comments:php array函数库知识强大, 熟练.加油
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