Blogger Information
Blog 19
fans 1
comment 0
visits 12237
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
数组的学习
▽空城旧梦
Original
507 people have browsed it

题目

  1. 如何将以下二维数组里的键值name换成user ,其他保持不变? $data = [[‘name’=>’zhangdan’,’id’=>2],[‘name’=>’lisi’,’id’=>1]];
  2. 生成一个由1-100组成的数组,要求返回该数组中的偶数组成的新数组,并且新数组的索引从0开始? (提示:你可能会用到的函数:range(),array_map,array_filter,array_values)?

答案

1.第一题

  1. $data = [['name'=>'zhangdan','id'=>2],['name'=>'lisi','id'=>1]];
  2. $arr = [];
  3. foreach ($data as $s => $v) {
  4. $arr[$s]['user'] = $data[$s]['name'];
  5. $arr[$s]['id'] = $data[$s]['id'];
  6. }
  7. echo "<pre>";
  8. $data = $arr;
  9. print_r($data);
  10. `

2.第二题

  1. $arr = range(1,100);
  2. function odd($var)
  3. {
  4. // 返回输入整数是否为奇数(单数)
  5. return $var & 1;
  6. };
  7. echo '<pre>';
  8. print_r(array_values(array_filter($arr, "odd")));
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