Blogger Information
Blog 41
fans 0
comment 0
visits 30985
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP数组常用函数
陈强
Original
692 people have browsed it

array_flip(array)

交换数组中键和值的位置,若重复前面的会被后面的覆盖

  1. $arr = ['name'=>'name','age'=>28,'gender'=>'male'];
  2. var_dump(array_flip($arr));
  3. //输出:array(3) { ["name"]=> string(4) "name" [28]=> string(3) "age" ["male"]=> string(6) "gender" }

array_change_key_case(array)

将数组中的键名改为全小写或大写array_change_key_case(array,case),case有两个常用量:CASE_UPPER或CASE_LOWER(默认值),即默认全小写

  1. $arr = ['name'=>'name','age'=>28,'gender'=>'male'];
  2. var_dump(array_change_key_case($arr,CASE_UPPER));
  3. //输出:array(3) { ["NAME"]=> string(4) "name" ["AGE"]=> int(28) ["GENDER"]=> string(4) "male" }

array_key_first(array)

得到数组的第一个键名

  1. $arr = ['name'=>'name','age'=>28,'gender'=>'male'];
  2. print_r(array_key_first($arr)); //输出键名:name

array_key_lase(array)

得到数组的第一个键名

  1. $arr = ['name'=>'name','age'=>28,'gender'=>'male'];
  2. print_r(array_key_last($arr)); //输出键名:gender

array_pop($array)

弹出最后一个元素值

  1. $arr = ['name'=>'name','age'=>28,'gender'=>'male'];
  2. print_r(array_pop($arr)); //输出键名:male

array_unique

去除数组中重复的值

  1. $nums = [1,2,3,3,2,1];
  2. print_r(array_unique($nums));
  3. //输出:Array ( [0] => 1 [1] => 2 [2] => 3 )

array_combine(keys,values)

创建一个数组,用一个数组的值作为其键名,另一个数组的值作为其值

  1. $arr1 = ['name','age','gender'];
  2. $arr2=['jack',28,'male'];
  3. print_r(array_combine($arr1,$arr2));
  4. //输出:Array ( [name] => jack [age] => 28 [gender] => male )

array_merge(array1,array2…)

合并一个或多个数组,键名相同时,若是字符键名则会被覆盖,数字键名则不会被覆盖,而是附加到后面

  1. $arr1 = ['name'=>'jack',2=>'age','gender'];
  2. $arr2=['name'=>'tom',2=>28,'male'];
  3. print_r(array_merge($arr1,$arr2));
  4. //输出:Array ( [name] => tom [0] => age [1] => gender [2] => 28 [3] => male )

array_fill_keys(keys,value)

使用指定的键和值填充数组

  1. $arr = [1,2,3,4,5];
  2. print_r(array_fill_keys($arr, 'apple'));
  3. //输出 :Array ( [1] => apple [2] => apple [3] => apple [4] => apple [5] => apple )

array_keys() array_values()

获取数组所有的键或值

  1. $arr = ['name','age','gender','email','salary'];
  2. print_r(array_keys($arr));
  3. echo '<br>';
  4. print_r(array_values($arr));

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
Author's latest blog post