Blogger Information
Blog 24
fans 0
comment 0
visits 18488
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
数组遍历与数组函数
昔年
Original
624 people have browsed it

一、数组遍历

$arr = [
    ['id' => '1020','name'=>'小明','grade' => 68],
    ['id' => '1030','name'=>'小红','grade' => 88],
    ['id' => '1040','name'=>'小张','grade' => 98],
];
foreach($arr as $k => $v){
    echo $k.'<br>';
    foreach($v as $key => $val){
        echo $key.':'.$val.'<br>';
    }
    echo '<hr>';
}

demo1.png

二、数组函数

//1.array_keys()获取所有键名组成的数组

$res = array_keys($arr[0]);
printf('<pre>%s<pre>',print_r($res,true));
echo '<br>';

//2.in_array():判断数组中是否存在某个值

$res = in_array('1030',$arr[2]);
echo $res;
var_dump($res);
echo '<hr>';

demo2.png

//3.array_column():获取多维数组中的一列组成新的数组

$res = array_column($arr,'name');
printf('<pre>%s<pre>',print_r($res,true));
echo '<hr>';

demo5.png

//4.array_merge():将多个数组合并成一个数组

$arr_1 = [
    ['id' => '1060','name'=>'小刘','grade'=>67],
    ['id' => '1070','name'=>'小宋','grade'=>75]
];
$arr_merge = array_merge($arr,$arr_1);
printf('<pre>%s<pre>',print_r($arr_merge,true));
echo '<hr>';

//5.1 implode():将数组元素按照指定字符串拼接成数组

//5.2 explode():将字符串分割为数组

$str_array = ['abc','ertt','wee','gh'];
$str = implode('-',$str_array);
echo $str.'<br>';
$str_array = explode('-',$str);
printf('<pre>%s<pre>',print_r($str_array,true));
echo '<hr>';

demo7.png

//6.range():根据指定范围生产数组,包含指定的元素

$res = range('a','f');
printf('<pre>%s<pre>',print_r($res,true));
echo '<hr>';[object Object]

//7.1 extract():将关联数组拆分成变量名值对

//7.2 compact():将一组变量名值对拼接成一个关联数组键值对

extract($arr[0]);
echo "id:$id&nbsp name:$name &nbsp grade:$grade";
$res = compact('id','name','grade');
printf('<pre>%s<pre>',print_r($res,true));
echo '<hr>';

demo9.png

// 8.1 array_intersect():计算数组的交集

// 8.2 array_intersect_assoc():计算数组的交集,键名也做比较

$grade_1 = ['css' => 72,'java' => 56,'php'=>87,'js'=>67];
$grade_2 = ['html/css' => 72,'java' => 56,'php'=>85,'js'=>67];
printf('<pre>%s<pre>',print_r(array_intersect($grade_1,$grade_2),true));
printf('<pre>%s<pre>',print_r(array_intersect_assoc($grade_1,$grade_2),true));
echo '<hr>';

//9 array_search(查找的值,数组, [, bool $strict = false] )在数组中搜索给定的值,如果成功则返回首个相应的键名, 

$arr = ['dgf','fdfd','AS','were','sddss',''];
$str_1 = 'AS';
$str_2 = null;
echo array_search($str_1,$arr).'<br>';
echo array_search($str_2,$arr).'<br>';
//如果第三个参数为true,则还要判断类型
echo array_search($str_2,$arr,true);
echo '<hr>';

demo10.png

//10 array_pad(array $array, int $size, mixed $value )  以指定长度将一个值填充进数组

$arr = ['dgf','fdfd','AS','were','sddss'];
printf('<pre>%s<pre>',print_r(array_pad($arr,7,'abc'),true));

demo11.png

三、回调函数

//1. array_filter():用回调规律数组中的单元,返回计算结果为true的元素组成的数组

$arr = [156,'php','bac',null,[3,4,5],(new class{}),456,true,'true'];
$res = array_filter($arr,function($items){
    return is_string($items);
});
printf('<pre>%s</pre><hr>',print_r($res,true));

//2. array_map():是为数组中的每个元素应用回调函数进行处理,返回新数据

$arr = [156,'php,aad,fd,dff','bac,rtt',null,[3,4,5],(new class{}),456,true,'true,fggg'];
$res = array_map(function($items){
    switch(gettype($items)){
        case 'int':
           $items += 100;
        case 'string':
            $items = explode(',',$items);
        break;
        case 'array':
            $items = implode('-',$items);
    }
    return $items;
},$arr);
printf('<pre>%s</pre><hr>',print_r($res,true));

// 3. array_reduce():迭代处理数组元素

$arr = [1,2,3,4,5,6,7];
$res = array_reduce($arr,function($sum,$items){
    $sum += $items;
    return $sum;
});
var_dump($res);
echo '<hr>';

//4. array_walk():使用自定义回调对数组中成员逐个处理,返回布尔值

//主要用于数组成员的格式化,不受数组指针影响

$arr = [1,2,3,4,5,6,7];
array_walk($arr,function($val){
    echo ($val+10);
    echo '<br>';
});

demo12.png


demo13.png

三、查询与替换

1.array_slice($arr,$offset,$length,$flag):从数组取出一部分

$stu = ['id'=>1020,'name' => 'Jack','department'=>'产品研发部','level' => 11,'job'=>'PHP开发工程师'];
$res = array_slice($stu,1,2);
printf('<pre>%s</pre><br>',print_r($res,true));
$res = array_slice($stu,-3,2);
printf('<pre>%s</pre><br>',print_r($res,true));
$res = array_slice($stu,2,-1,false);
printf('<pre>%s</pre><br>',print_r($res,true));
$res = array_slice($stu,2,-1,true);
printf('<pre>%s</pre><br>',print_r($res,true));
echo '<hr>';

demo14.png

2.array_splice($arr):删除数组中的一部分

//可以实现对数组的:删除,替换/更新,添加
$arr = [32,45,67,254,35,63,37,93];
//删除元素
// array_splice($arr,起始索引,删除数量),返回的是被删除元素组成的数组
$res_1 = array_splice($arr,3,3);
$res_2 = array_splice($arr,-3,-1);
printf('<pre>%s</pre>',print_r($res_1,true));
printf('<pre>%s</pre>',print_r($res_2,true));
echo '<hr>';
//替换元素
$arr = [32,45,67,254,35,63,37,93];
$res = array_splice($arr,2,2,[56,22]);
printf('<pre>%s</pre>',print_r($arr,true));
printf('<pre>%s</pre>',print_r($res,true));
echo '<hr>';
$arr = [32,45,67,254,35,63,37,93];
//添加元素
$res = array_splice($arr,2,0,[91,34]);
printf('<pre>%s</pre>',print_r($arr,true));
echo '<hr>';

demo15.png



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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!