使用函数array_keys(),得到数组中所有的键,参数:数组
<span style="color: #800080;">$arr</span>=<span style="color: #0000ff;">array</span><span style="color: #000000;">(); </span><span style="color: #800080;">$arr</span>['one']="one"<span style="color: #000000;">; </span><span style="color: #800080;">$arr</span>['two']="two"<span style="color: #000000;">; </span><span style="color: #800080;">$arr</span>['three']="three"<span style="color: #000000;">; </span><span style="color: #800080;">$newArr</span>=<span style="color: #008080;">array_keys</span>(<span style="color: #800080;">$arr</span><span style="color: #000000;">); </span><span style="color: #008080;">print_r</span>(<span style="color: #800080;">$newArr</span><span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;">Array ( [0] => one [1] => two [2] => three )</span>
使用函数array_values(),得到数组中所有的值,参数:数组
<span style="color: #800080;">$arr</span>=<span style="color: #0000ff;">array</span><span style="color: #000000;">(); </span><span style="color: #800080;">$arr</span>[20]="one"<span style="color: #000000;">; </span><span style="color: #800080;">$arr</span>[30]="two"<span style="color: #000000;">; </span><span style="color: #800080;">$arr</span>[40]="three"<span style="color: #000000;">; </span><span style="color: #800080;">$newArr</span>=<span style="color: #008080;">array_values</span>(<span style="color: #800080;">$arr</span><span style="color: #000000;">); </span><span style="color: #008080;">print_r</span>(<span style="color: #800080;">$newArr</span><span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;">Array ( [0] => one [1] => two [2] => three )</span>
使用函数array_map(),使每个元素都调用一下自定义函数,参数:String类型函数名称,数组
<span style="color: #800080;">$arr</span>=<span style="color: #0000ff;">array</span><span style="color: #000000;">(); </span><span style="color: #800080;">$arr</span>[0]="one"<span style="color: #000000;">; </span><span style="color: #800080;">$arr</span>[1]="two"<span style="color: #000000;">; </span><span style="color: #800080;">$arr</span>[2]="three"<span style="color: #000000;">; </span><span style="color: #0000ff;">function</span> test(<span style="color: #800080;">$v</span><span style="color: #000000;">){ </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$v</span>." Hello"<span style="color: #000000;">; } </span><span style="color: #800080;">$newArr</span>=<span style="color: #008080;">array_map</span>("test",<span style="color: #800080;">$arr</span><span style="color: #000000;">); </span><span style="color: #008080;">print_r</span>(<span style="color: #800080;">$newArr</span><span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;">Array ( [0] => one Hello [1] => two Hello [2] => three Hello )</span>
使用函数array_merge(),把两个数组合并成一个,参数:数组,数组
关联数组合并时,键相同的会被后面的数组覆盖
索引数组合并时,会连接在一起形成一个新的数组
<span style="color: #800080;">$arr</span>=<span style="color: #0000ff;">array</span><span style="color: #000000;">(); </span><span style="color: #800080;">$arr</span>[0]="one"<span style="color: #000000;">; </span><span style="color: #800080;">$arr</span>[1]="two"<span style="color: #000000;">; </span><span style="color: #800080;">$arr</span>[2]="three"<span style="color: #000000;">; </span><span style="color: #800080;">$arr1</span>=<span style="color: #0000ff;">array</span><span style="color: #000000;">(); </span><span style="color: #800080;">$arr</span>[3]="taoshihan1"<span style="color: #000000;">; </span><span style="color: #800080;">$arr</span>[4]="taoshihan2"<span style="color: #000000;">; </span><span style="color: #800080;">$arr</span>[5]="taoshihan3"<span style="color: #000000;">; </span><span style="color: #800080;">$newArr</span>=<span style="color: #008080;">array_merge</span>(<span style="color: #800080;">$arr</span>,<span style="color: #800080;">$arr1</span><span style="color: #000000;">); </span><span style="color: #008080;">print_r</span>(<span style="color: #800080;">$newArr</span><span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;">Array ( [0] => one [1] => two [2] => three [3] => taoshihan1 [4] => taoshihan2 [5] => taoshihan3 )</span> <span style="color: #800080;">$arr</span>=<span style="color: #0000ff;">array</span>("one","two","three"<span style="color: #000000;">); </span><span style="color: #800080;">$arr1</span>=<span style="color: #0000ff;">array</span>("4","5","6"<span style="color: #000000;">); </span><span style="color: #800080;">$newArr</span>=<span style="color: #008080;">array_merge</span>(<span style="color: #800080;">$arr</span>,<span style="color: #800080;">$arr1</span><span style="color: #000000;">); </span><span style="color: #008080;">print_r</span>(<span style="color: #800080;">$newArr</span><span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;">Array ( [0] => one [1] => two [2] => three [3] => 4 [4] => 5 [5] => 6 )</span>
使用函数ksort(),按照键名进行排序,注意没有返回新的数组,还是原来的数组
<span style="color: #800080;">$arr</span>=<span style="color: #0000ff;">array</span>("2"=>"taoshihan2","1"=>"taoshihan1","3"=>"taoshihan3"<span style="color: #000000;">); </span><span style="color: #008080;">ksort</span>(<span style="color: #800080;">$arr</span><span style="color: #000000;">); </span><span style="color: #008080;">print_r</span>(<span style="color: #800080;">$arr</span><span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;">Array ( [1] => taoshihan1 [2] => taoshihan2 [3] => taoshihan3 )</span>
使用函数array_search(),搜索某个键值,返回对应的键
<span style="color: #800080;">$arr</span>=<span style="color: #0000ff;">array</span>("2"=>"taoshihan2","1"=>"taoshihan1","3"=>"taoshihan3"<span style="color: #000000;">); </span><span style="color: #0000ff;">echo</span> <span style="color: #008080;">array_search</span>("taoshihan1",<span style="color: #800080;">$arr</span><span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;"> 1</span>