PHP中常用的数组操作方法笔记整理(2)

<?php //array_pad函数,数组数组首尾选择性追加 $num = array(1=>10,2=>20,3=>30); $num = array_pad($num,4,40); print_r($num);//Array ( [0] => 10 [1] => 20 [2] => 30 [3] => 40 ) $num = array_pad($num,-5,50);//array_pad(array,size,value) print_r($num);//Array ( [0] => 50 [1] => 10 [2] => 20 [3] => 30 [4] => 40 ) ?>

size:指定的长度。整数则填补到右侧,负数则填补到左侧。

unset()的使用

<?php //unset()的使用 $num = array_fill(0,5,rand(1,10));//rand(min,max) print_r($num);//Array ( [0] => 8 [1] => 8 [2] => 8 [3] => 8 [4] => 8 ) echo '<br/>'; unset($num[3]); print_r($num);//Array ( [0] => 8 [1] => 8 [2] => 8 [4] => 8 ) ?>

array_fill()的使用

<?php //array_fill()的使用 $num = range('a','e'); $arrayFilled = array_fill(1,2,$num);//array_fill(start,number,value) echo '<pre>'; print_r($arrayFilled); ?>

array_combine()的使用

<?PHP $number = array(1,2,3,4,5); $array = array("I","Am","A","PHP","er"); $newArray = array_combine($number,$array); print_r($newArray);//Array ( [1] => I [2] => Am [3] => A [4] => PHP [5] => er ) ?>

array_splice()删除数组成员

<?php $color = array("red", "green", "blue", "yellow"); count ($color); //得到4 array_splice($color,1,1); //删除第二个元素 print_r(count ($color)); //3 echo $color[2]; //yellow echo $color[1]; //blue ?>

array_unique删除数组中的重复值

<?php $color=array("red", "green", "blue", "yellow","blue","green"); $result = array_unique($color); print_r($result);//Array ( [0] => red [1] => green [2] => blue [3] => yellow ) ?>

array_flip()交换数组的键值和值

<?PHP $array = array("red","blue","red","Black"); print_r($array); echo "<br />"; $array = array_flip($array);// print_r($array);//Array ( [red] => 2 [blue] => 1 [Black] => 3 ) ?>

array_search()搜索数值

<meta charset="utf-8"> <?php $array = array("red","blue","red","Black"); $result=array_search("red",$array)//array_search(value,array,strict) if(($result === NULL)){ echo "不存在数值red"; }else{ echo "存在数值 $result";//存在数值 0 } ?>

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/668a441eb7932c414eaefb4001fa448e.html