博客列表 >数组函数的认识及回调函数等知识

数组函数的认识及回调函数等知识

emy
emy原创
2020年05月06日 01:02:22706浏览

一、10个数组函数

<?php
// 数组函数实例
// 1.array_unique :移除数组中重复的值

$color = array("a" => "green", "red", "b" => "green", "blue", "red");
$result = array_unique($color);
printf('原数组:<pre>%s</pre>',print_r($color,true));
printf('1-移除重复值:<pre>%s</pre>',print_r(array_unique($color),true));
// 数组函数实例
echo '<hr>';

//2.count()获得数组的长度
$colorvar=array("green","red","red");
echo '2-总共:'.count($colorvar);
echo '<hr>';

//3.array_flip:交换数组中的键和值
$color2 = array("green", "red", "blue");
$flipped = array_flip($color2);
printf('3-颜色键与值交换:<pre>%s</pre>',print_r(array_flip($flipped),true));
echo '<hr>';

//4.array_key_exists:检查某个数组中是否存在指定的键名,如果键名存在则返回 true,如果键名不存在则返回 false。
$a=array("green"=>"car","red"=>"book");
if (array_key_exists("red",$a))
  {
  echo "4-我存在!";
  }
else
  {
  echo "4-我不存在!";
  }
echo '<hr>';

//5.array_pad:将指定数量的带有指定值的元素插入到数组中。
$a2=array("red","green");
printf('5-颜色键与值交换:<pre>%s</pre>',print_r(array_pad($a2,5,"blue"),true));
echo '<hr>';

//6.array_replace_recursive:函数递归地使用后面数组的值替换第一个数组的值。
$a3=array("a"=>array("red"),"b"=>array("green","blue"),);
$a4=array("a"=>array("white"),"b"=>array("black"));
printf('6-结果:<pre>%s</pre>',print_r(array_replace_recursive($a3,$a4),true));
echo '<hr>';

//7.next:函数将内部指针指向数组中的下一个元素,并输出。
$student = array("may", "bill", "apple");
echo '7-结果:'.current($student) . "<br>";
echo next($student);
echo '<hr>';

//8. $arraykey、$arrayval:合并2个数组.
$arrkey = ['color1','color2','color3'];
$arrval = ['white','blue','red'];
$hebing = array_combine($arrkey,$arrval);
printf('8-结果:<pre>%s</pre>',print_r($hebing,true));
echo '<hr>';

//9. array_chunk:把一个数组分割成2个元素组成的数组.
$student2 = array("may", "bill", "apple");
$fenge = array_chunk($student2,2);
printf('9-结果:<pre>%s</pre>',print_r($fenge,true));
echo '<hr>';

//10. array_key_last,array_key_first:获取第一/最后一个数组
$student3 = array("may", "bill", "apple");
$a5 = array_key_last($student3);
$a6 = array_key_first($student3);
echo '10-最后一个键名 :' . $a5,'<br>';
echo '10-第一个键名 :' . $a6;

输出图:

1.jpg

二、四个数组回调函数举例

<?php
    //数组回调函数
    // 1.array_filter():过滤数组中的元素,返回值为true的数组元素。
    $zu1 = [78,'newer',true,[1,3,5],(new class{}),0,'0','',null,false];
    printf('1-过滤前的数组:<pre>%s</pre>',print_r($zu1,true));
    $res = array_filter($zu1);
    printf('1-过滤后的数组:<pre>%s</pre>',print_r($res,true));
    // 返回标量即单值元素的数组:
    $res1 = array_filter($zu1,function($res2){
        return is_scalar($res2);
    });
    printf('1-过滤非单值元素后的数组:<pre>%s</pre>',print_r($res1,true));
    // 2.把数组、对象的元素、属性转换为字符串
    $zu2 = [78,'newer',true,[1,3,5],(new class{public $name='姓名';public $total  = '1000';}),0,'0','',null,false];
    $res2 = array_map(function($item){
        switch (gettype($item)) {
            case 'object':
                $item = get_object_vars($item);
            case 'array':
                $item = implode(', ', $item);
        }
        return $item;
        }, $zu2);
    printf('2-转换非单值元素后的数组:<pre>%s</pre>',print_r($res2,true));

    // array_map(): 同时处理多个数组
    $key = ['host', 'username', 'password'];
    $values = ['localhost', 'admin', '999999'];
    $res = array_map(function($value1, $value2) 
    {
        return [$value1 => $value2];
    }, $key, $values);
    printf('<pre>%s</pre><hr>', print_r($res, true));


// 3. array_reduce(): 迭代处理数组元素
    $res=array_reduce($res, function($prev, $current){
    // 获取当前元素的键
    $key = key($current);
    // 获取当前元素的值
    $value = current($current);
    // 拼装成键值对
    $prev[$key] = $value;
    return $prev;
    });
    printf('3-内置的数组迭代:<pre>%s</pre><hr>', print_r($res, true));
// 4. array_walk(): 使用自定义回调对数组中成员逐个处理,返回布尔值
// 主要用于数组成员的格式化, 不受数组指针影响
    $res33 = ['id'=>1, 'name'=>'emy', 'job'=>'none'];
// array_walk(数组, 回调,回调的第三个数组的默认值) 
    array_walk($res33, function($value, $key, $color) {
    printf('4-'.'[ %s ] => <span style="color:%s">%s</span><br>',$key,$color, $value );
    },'red');

运行结果:

2.jpg

三、array_slice()与array_splice()实例演示

<?php

//1-array_slice() 从数组中取出一段

    $res1 = ['0white', '1black', '2red', '3blue'];
    $aa = array_slice($res1, 2);     // 从第3个开始取全部
    $aa = array_slice($res1, 0, 2);  // 从第1个开始取2个
    $aa = array_slice($res1, -1, 1); // 取最后1个
    $aa = array_slice($res1, -1, 1, true);  // 取最后1个,且键值不改变
    printf('<pre>%s</pre>', print_r($res1, true));
    echo '<hr>';
//2-array_splice() 去掉数组中的某一部分并用其它值取代

    $res2 = ['0white', '1black', '2red', '3blue'];
// 去掉最后1个
    $bb = array_splice($res2, -1);
    // 只保留第1个
    $bb = array_splice($res2, 0, 1);
    // 去掉中间,只保留两头的两个元素
    $bb = array_splice($res2, 1, -1);
    printf('<pre>%s</pre>', print_r($res2, true));

运行结果:

3.jpg

四、总结:需要对课程反复听,有点疑惑:不知道在哪个场景下用这些回调。

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议