博客列表 >[0812] 5种数组函数

[0812] 5种数组函数

P粉912642467
P粉912642467原创
2022年08月12日 15:39:28351浏览

array_chuck()

  1. $stu = [
  2. 'id' => 101,
  3. 'name' => '无忌',
  4. 'age' => 20,
  5. 'course' => 'php',
  6. 'grade' => 80
  7. ];
  8. $fn1 = array_chunk($stu,2);
  9. printf("<pre>%s</pre>",print_r($fn1,true));
  10. foreach($fn1 as $singleStudent){
  11. foreach($singleStudent as $detail){
  12. echo $detail."<br>";
  13. }
  14. }
  15. echo "<hr>";

array_combine()

  1. $number = ['1','2','3'];
  2. $student = ['Steve','Carl','Jessica'];
  3. $newClass = array_combine($number,$student);
  4. foreach($newClass as $key=>$singleStudent){
  5. printf('[ %s ] => %s<br>', $key, $singleStudent);
  6. }
  7. echo "<hr>";

array_map()

  1. $number2 = ['4','5','6'];
  2. function doubleNum($num){
  3. return $num *2;
  4. }
  5. $afterMap = array_map('doubleNum',$number2);
  6. foreach ($afterMap as $singleNum) {
  7. echo $singleNum."<br>";
  8. }

echo “<hr>“;

array_merge()

  1. $student2 = ['Leo','Jeff','Dean'];
  2. $studentGroup = array_merge($student, $student2);
  3. foreach($studentGroup as $singleStudent){
  4. echo $singleStudent."<br>";
  5. }
  6. echo "<hr>";

array_walk()

  1. $fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
  2. ksort($fruits);
  3. function addFruit($putInFruit, $key){
  4. echo $key.":".$putInFruit."<br>";
  5. }
  6. array_walk($fruits,'addFruit');
  7. // printf("<pre>%s</pre>",print_r($newFruits,true));
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议