[PHP] 重回基础(Array相关函数),重回array_PHP教程

WBOY
Freigeben: 2016-07-12 08:52:29
Original
783 Leute haben es durchsucht

[PHP] 重回基础(Array相关函数),重回array

使用函数array_keys(),得到数组中所有的键,参数:数组

<span>$arr</span>=<span>array</span><span>();
</span><span>$arr</span>['one']="one"<span>;
</span><span>$arr</span>['two']="two"<span>;
</span><span>$arr</span>['three']="three"<span>;

</span><span>$newArr</span>=<span>array_keys</span>(<span>$arr</span><span>);
</span><span>print_r</span>(<span>$newArr</span><span>);
</span><span>//</span><span>Array ( [0] => one [1] => two [2] => three )</span>
Nach dem Login kopieren

使用函数array_values(),得到数组中所有的值,参数:数组

<span>$arr</span>=<span>array</span><span>();
</span><span>$arr</span>[20]="one"<span>;
</span><span>$arr</span>[30]="two"<span>;
</span><span>$arr</span>[40]="three"<span>;

</span><span>$newArr</span>=<span>array_values</span>(<span>$arr</span><span>);
</span><span>print_r</span>(<span>$newArr</span><span>);
</span><span>//</span><span>Array ( [0] => one [1] => two [2] => three )</span>
Nach dem Login kopieren

使用函数array_map(),使每个元素都调用一下自定义函数,参数:String类型函数名称,数组

<span>$arr</span>=<span>array</span><span>();
</span><span>$arr</span>[0]="one"<span>;
</span><span>$arr</span>[1]="two"<span>;
</span><span>$arr</span>[2]="three"<span>;
</span><span>function</span> test(<span>$v</span><span>){
    </span><span>return</span> <span>$v</span>." Hello"<span>;
}
</span><span>$newArr</span>=<span>array_map</span>("test",<span>$arr</span><span>);
</span><span>print_r</span>(<span>$newArr</span><span>);
</span><span>//</span><span>Array ( [0] => one Hello [1] => two Hello [2] => three Hello )</span>
Nach dem Login kopieren

使用函数array_merge(),把两个数组合并成一个,参数:数组,数组

关联数组合并时,键相同的会被后面的数组覆盖

索引数组合并时,会连接在一起形成一个新的数组

<span>$arr</span>=<span>array</span><span>();
</span><span>$arr</span>[0]="one"<span>;
</span><span>$arr</span>[1]="two"<span>;
</span><span>$arr</span>[2]="three"<span>;

</span><span>$arr1</span>=<span>array</span><span>();
</span><span>$arr</span>[3]="taoshihan1"<span>;
</span><span>$arr</span>[4]="taoshihan2"<span>;
</span><span>$arr</span>[5]="taoshihan3"<span>;

</span><span>$newArr</span>=<span>array_merge</span>(<span>$arr</span>,<span>$arr1</span><span>);
</span><span>print_r</span>(<span>$newArr</span><span>);
</span><span>//</span><span>Array ( [0] => one [1] => two [2] => three [3] => taoshihan1 [4] => taoshihan2 [5] => taoshihan3 )</span>

<span>$arr</span>=<span>array</span>("one","two","three"<span>);
</span><span>$arr1</span>=<span>array</span>("4","5","6"<span>);
</span><span>$newArr</span>=<span>array_merge</span>(<span>$arr</span>,<span>$arr1</span><span>);
</span><span>print_r</span>(<span>$newArr</span><span>);
</span><span>//</span><span>Array ( [0] => one [1] => two [2] => three [3] => 4 [4] => 5 [5] => 6 )</span>
Nach dem Login kopieren

使用函数ksort(),按照键名进行排序,注意没有返回新的数组,还是原来的数组

<span>$arr</span>=<span>array</span>("2"=>"taoshihan2","1"=>"taoshihan1","3"=>"taoshihan3"<span>);
</span><span>ksort</span>(<span>$arr</span><span>);
</span><span>print_r</span>(<span>$arr</span><span>);
</span><span>//</span><span>Array ( [1] => taoshihan1 [2] => taoshihan2 [3] => taoshihan3 )</span>
Nach dem Login kopieren

使用函数array_search(),搜索某个键值,返回对应的键

<span>$arr</span>=<span>array</span>("2"=>"taoshihan2","1"=>"taoshihan1","3"=>"taoshihan3"<span>);
</span><span>echo</span> <span>array_search</span>("taoshihan1",<span>$arr</span><span>);
</span><span>//</span><span> 1</span>
Nach dem Login kopieren

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1126176.htmlTechArticle[PHP] 重回基础(Array相关函数),重回array 使用函数array_keys(),得到数组中所有的键,参数:数组 $arr = array (); $arr ['one']="one" ; $arr ['two']=...
Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage