Correcting teacher:PHPz
Correction status:qualified
Teacher's comments:
<?php
//array_change_key_case(array $array,int $case):array;将数组的所有键名转为小写或者大写
//$case :CASE_LOWER 小写;CASE_UPPER()大写
$arr =['Id'=>001,'Name'=>'小红','Age'=>18,'Sex'=>'0'];
print_r($arr); //Array ( [Id] => 1 [Name] => 小红 [Age] => 18 [Sex] => 0 )
print_r(array_change_key_case($arr,CASE_LOWER)); //Array ( [id] => 1 [name] => 小红 [age] => 18 [sex] => 0 )
print_r(array_change_key_case($arr,CASE_UPPER)); //Array ( [ID] => 1 [NAME] => 小红 [AGE] => 18 [SEX] => 0 )
//array_chunk(array $array,int $length,bool $preserver_keys = false):array 将一个数组分割成多个
//$preserver_keys 设为true,可以使PHP保留原数组的键名,设为false,则重新数字索引.
$arr = range(0,50,3);
print_r($arr); //Array ( [0] => 0 [1] => 3 [2] => 6 [3] => 9 [4] => 12 [5] => 15 [6] => 18 [7] => 21 [8] => 24 [9] => 27 [10] => 30 [11] => 33 [12] => 36 [13] => 39 [14] => 42 [15] => 45 [16] => 48 )
print_r(array_chunk($arr,5)); // Array ( [0] => Array ( [0] => 0 [1] => 3 [2] => 6 [3] => 9 [4] => 12 ) [1] => Array ( [0] => 15 [1] => 18 [2] => 21 [3] => 24 [4] => 27 ) [2] => Array ( [0] => 30 [1] => 33 [2] => 36 [3] => 39 [4] => 42 ) [3] => Array ( [0] => 45 [1] => 48 ) )
//array_column(array $array,int|string|null $column_key,int|string|null $index_key=null):array
//返回输入数张中指定列的值;
$arr =[
['Id'=>001,'Name'=>'小红','Age'=>18,'Sex'=>'0'],
['Id'=>002,'Name'=>'小明','Age'=>28,'Sex'=>'1'],
['Id'=>003,'Name'=>'小绿','Age'=>38,'Sex'=>'0']
];
print_r($arr); // Array ( [0] => Array ( [Id] => 1 [Name] => 小红 [Age] => 18 [Sex] => 0 ) [1] => Array ( [Id] => 2 [Name] => 小明 [Age] => 28 [Sex] => 1 ) [2] => Array ( [Id] => 3 [Name] => 小绿 [Age] => 38 [Sex] => 0 ) )
print_r(array_column($arr,null,'Id')); //Array ( [1] => Array ( [Id] => 1 [Name] => 小红 [Age] => 18 [Sex] => 0 ) [2] => Array ( [Id] => 2 [Name] => 小明 [Age] => 28 [Sex] => 1 ) [3] => Array ( [Id] => 3 [Name] => 小绿 [Age] => 38 [Sex] => 0 ) )
print_r(array_column($arr,'Name','Id')); //Array ( [1] => 小红 [2] => 小明 [3] => 小绿 )
//array_reduce(raay $array,callable $callback,mixed $initial = null):mixed
//用回调函数迭代地将数组简化为单一的值
$arr =[
['Id'=>001,'Name'=>'小红','Age'=>18,'Sex'=>'0'],
['Id'=>002,'Name'=>'小明','Age'=>28,'Sex'=>'1'],
['Id'=>003,'Name'=>'小绿','Age'=>38,'Sex'=>'0']
];
function fun1($carry,$item){
$carry += $item['Age'];
return $carry;
}
$result = array_reduce($arr,'fun1');
print_r($result); //84
//array_map(callbale $callback,array $array,array...$arrays):array
//为数组的每个元素应用回调函数
function fun2($item){
//每个人都加上2岁
$item['Age'] += 2;
return $item;
}
$result = array_map('fun2',$arr);
print_r($result); //Array ( [0] => Array ( [Id] => 1 [Name] => 小红 [Age] => 20 [Sex] => 0 ) [1] => Array ( [Id] => 2 [Name] => 小明 [Age] => 30 [Sex] => 1 ) [2] => Array ( [Id] => 3 [Name] => 小绿 [Age] => 40 [Sex] => 0 ) )
//array_filter(array $array,?callabel $callback = null, int $mode = 0):array
//使用回调函数过滤数组的元素
function fun3($item){
//过滤掉小明
if($item['Id'] != 002){
return $item;
}
}
$result = array_filter($arr,'fun3');
print_r($result); //Array ( [0] => Array ( [Id] => 1 [Name] => 小红 [Age] => 18 [Sex] => 0 ) [2] => Array ( [Id] => 3 [Name] => 小绿 [Age] => 38 [Sex] => 0 ) )