Correcting teacher:天蓬老师
Correction status:qualified
Teacher's comments:
交换数组中键和值的位置,若重复前面的会被后面的覆盖
$arr = ['name'=>'name','age'=>28,'gender'=>'male'];
var_dump(array_flip($arr));
//输出:array(3) { ["name"]=> string(4) "name" [28]=> string(3) "age" ["male"]=> string(6) "gender" }
将数组中的键名改为全小写或大写array_change_key_case(array,case),case有两个常用量:CASE_UPPER或CASE_LOWER(默认值),即默认全小写
$arr = ['name'=>'name','age'=>28,'gender'=>'male'];
var_dump(array_change_key_case($arr,CASE_UPPER));
//输出:array(3) { ["NAME"]=> string(4) "name" ["AGE"]=> int(28) ["GENDER"]=> string(4) "male" }
得到数组的第一个键名
$arr = ['name'=>'name','age'=>28,'gender'=>'male'];
print_r(array_key_first($arr)); //输出键名:name
得到数组的第一个键名
$arr = ['name'=>'name','age'=>28,'gender'=>'male'];
print_r(array_key_last($arr)); //输出键名:gender
弹出最后一个元素值
$arr = ['name'=>'name','age'=>28,'gender'=>'male'];
print_r(array_pop($arr)); //输出键名:male
去除数组中重复的值
$nums = [1,2,3,3,2,1];
print_r(array_unique($nums));
//输出:Array ( [0] => 1 [1] => 2 [2] => 3 )
创建一个数组,用一个数组的值作为其键名,另一个数组的值作为其值
$arr1 = ['name','age','gender'];
$arr2=['jack',28,'male'];
print_r(array_combine($arr1,$arr2));
//输出:Array ( [name] => jack [age] => 28 [gender] => male )
合并一个或多个数组,键名相同时,若是字符键名则会被覆盖,数字键名则不会被覆盖,而是附加到后面
$arr1 = ['name'=>'jack',2=>'age','gender'];
$arr2=['name'=>'tom',2=>28,'male'];
print_r(array_merge($arr1,$arr2));
//输出:Array ( [name] => tom [0] => age [1] => gender [2] => 28 [3] => male )
使用指定的键和值填充数组
$arr = [1,2,3,4,5];
print_r(array_fill_keys($arr, 'apple'));
//输出 :Array ( [1] => apple [2] => apple [3] => apple [4] => apple [5] => apple )
获取数组所有的键或值
$arr = ['name','age','gender','email','salary'];
print_r(array_keys($arr));
echo '<br>';
print_r(array_values($arr));