php数组操作实例
本文章主要针对数组交集、判断键是否存在于数组中、合并两个数组、数据排序、增加数组,删除数组元素、随机抽取数组元素等实例,实例代码如下:
- */
- $array1=array("a"=>"green","red","blue");
- $array2=array("b"=>"green","yellow","red");
- $result=array_intersect($array1,$array2);
- print_r($result);
-
- $search_array=array('first'=>1,'second'=>4);
- if(array_key_exists('first',$search_array))
- {
- echo "the 'first' element is in the array";
- }
-
- $ar1=array("color"=>array("favorite"=>"red"),5);
- $ar2=array(10,"color"=>array("favorite"=>"green","blue"));
- $result=array_merge_recursive($ar1,$ar2);
- print_r($result);
-
- $array1=array("color"=>"red",2,4);
- $array2=array("a","b","color"=>"green","shape"=>"trapezoid",4);
- $result=array_merge($array1,$array2);
- print_r($result);
-
- $ar1=array("10",100,100,"a");
- $ar2=array(1,3,"2",1);
- array_multisort($ar1,$ar2);
- var_dump($ar1);
- var_dump($ar2);
-
- $stack=array("orange","banana","apple","rasp教程berry");
- $fruit=array_pop($stack);
- print_r($stack);
-
- $stack=array("orange", "banana");
- array_push($stack,"apple","raspberry");
- print_r($stack);
-
- srand((float)microtime()*10000000);
- $input=array("neo","morpheus","trinity","cypher","tank");
- $rand_keys=array_rand($input,2);
- print $input[$rand_keys[0]]."n";
- print $input[$rand_keys[1]]."n";