Blogger Information
Blog 52
fans 1
comment 1
visits 38279
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
如何将以下二维数组里的键值name换成user ,其他保持不变?
小丑0o鱼
Original
516 people have browsed it

1. 如何将以下二维数组里的键值name换成user ,其他保持不变?

  1. $data = [[‘name’=>’zhangdan’,’id’=>2],[‘name’=>’lisi’,’id’=>1]];
  2. $data = array_map(function ($item) {
  3. $a = array_replace(array_keys($item),[ 0=>'user']);
  4. return array_combine($a,array_values($item));
  5. }, $data);
  6. var_dump($data); //[['user'=>'zhangdan','id'=>2],['user'=>'lisi','id'=>1]]
  7. 2. 生成一个由1-100组成的数组,要求返回该数组中的偶数组成的新数组,并且新数组的索引从0开始? (提示:你可能会用到的函数:range(), array_map, array_filter, array_values)?
  8. // 通过range()生成数组
  9. $arr = range(1,100);
  10. //方法一,使用array_map
  11. $arr2 =[];
  12. array_map(function ($item) use (&$arr2) {
  13. if (($item % 2) === 0):
  14. $arr2[] = $item;
  15. endif;
  16. },$arr);
  17. echo "<pre>";
  18. print_r($arr2);
  19. //方法二
  20. // 回调直接使用到array_filter中也 可实现
  21. $arr4 = array_values(array_filter($arr, function ($item) {
  22. if (($item % 2) === 0):
  23. return $item;
  24. endif;
  25. }));
  26. echo "<pre>";
  27. print_r($arr4);
Correction status:Uncorrected

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!