[PHP] 重回基础(Array相关函数)
使用函数array_keys(),得到数组中所有的键,参数:数组 =['one']="one"['two']="two"['three']="three"<span style="color: #800080;">$newArr=<span style="color: #008080;">array_keys(<span style="color: #800080;">$arr<span style="color: #000000;">);<span style="color: #008080;">print_r(<span style="color: #800080;">$newArr<span style="color: #000000;">); <span style="color: #008000;">//<span style="color: #008000;">Array ( [0] => one [1] => two [2] => three ) 使用函数array_values(),得到数组中所有的值,参数:数组 =[20]="one"[30]="two"[40]="three"<span style="color: #800080;">$newArr=<span style="color: #008080;">array_values(<span style="color: #800080;">$arr<span style="color: #000000;">);<span style="color: #008080;">print_r(<span style="color: #800080;">$newArr<span style="color: #000000;">); <span style="color: #008000;">//<span style="color: #008000;">Array ( [0] => one [1] => two [2] => three ) 使用函数array_map(),使每个元素都调用一下自定义函数,参数:String类型函数名称,数组 =[0]="one"[1]="two"[2]="three" test( ." Hello"=("test",( one Hello [1] => two Hello [2] => three Hello )使用函数array_merge(),把两个数组合并成一个,参数:数组,数组 关联数组合并时,键相同的会被后面的数组覆盖 索引数组合并时,会连接在一起形成一个新的数组 =[0]="one"[1]="two"[2]="three"<span style="color: #800080;">$arr1=<span style="color: #0000ff;">array<span style="color: #000000;">();<span style="color: #800080;">$arr[3]="taoshihan1"<span style="color: #000000;">; <span style="color: #800080;">$arr[4]="taoshihan2"<span style="color: #000000;">; <span style="color: #800080;">$arr[5]="taoshihan3"<span style="color: #000000;">; <span style="color: #800080;">$newArr=<span style="color: #008080;">array_merge(<span style="color: #800080;">$arr,<span style="color: #800080;">$arr1<span style="color: #000000;">); <span style="color: #800080;">$arr=<span style="color: #0000ff;">array("one","two","three"<span style="color: #000000;">); 使用函数ksort(),按照键名进行排序,注意没有返回新的数组,还是原来的数组 =("2"=>"taoshihan2","1"=>"taoshihan1","3"=>"taoshihan3"(( taoshihan1 [2] => taoshihan2 [3] => taoshihan3 )使用函数array_search(),搜索某个键值,返回对应的键 =("2"=>"taoshihan2","3"=>"taoshihan3" ("taoshihan1",(编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |