if($num > 10){
//array_slice($arr,起始位置,截取长度,保留索引(默认为false))
$arr_firstpart = array_slice($arr,0,$num-5,true);
$arr_lastpart = array_slice($arr,($num-5),5,true);
}else{
echo "数组不超过10个元素,请重新输入";
exit();
}
//拼接
$arr_new = array_merge($arr_lastpart,$arr_firstpart);
return $arr_new;
}
$arr = array("a"=>1,2,3,8,9,6,"b"=>5,-1,"c"=>8,0,7);
echo "<pre>";
print_r($arr);
echo "<br>= = = = = 拼接后 = = = = <br><br>";
print_r(arrsort($arr));
echo "</pre>";
输出:
复制代码 代码如下:
Array
(
[a] => 1
[0] => 2
[1] => 3
[2] => 8
[3] => 9
[4] => 6
[b] => 5
[5] => -1
[c] => 8
[6] => 0
[7] => 7
)
= = = = = 拼接后 = = = =
复制代码 代码如下:
Array
(
[b] => 5
[0] => -1
[c] => 8
[1] => 0
[2] => 7
[a] => 1
[3] => 2
[4] => 3
[5] => 8
[6] => 9
[7] => 6
)
当数组不满足长度为10时:
复制代码 代码如下:
$arr = array("a"=>1,2,3);
输出:
复制代码 代码如下:
Array
(
[a] => 1
[0] => 2
[1] => 3
)
= = = = = 拼接后 = = = =
数组不超过10个元素,请重新输入
7.将两个数组连接成一个新数组.
方法①使用array_merge()函数
复制代码 代码如下:
array_merge($arr1,$arr2);
方法②使用array_merge_recursive()函数递归追加数组
( array_merge_recursive() 函数与 array_merge() 函数 一样,将一个或多个数组的元素的合并起来,一个数组中的值附加在前一个数组的后面。并返回作为结果的数组。
但是,与 array_merge() 不同的是,当有重复的键名时,值不会被覆盖,而是将多个相同键名的值递归组成一个数组。)
复制代码 代码如下:
<?php
$arr = array("a"=>1,"b"=>2,3);
$arr2 = array("a"=>Dee,3,5);
$arr3 = array_merge($arr,$arr2);
$arr4 = array_merge_recursive($arr,$arr2);
echo "<pre>";
print_r($arr3);
echo "<br> = = = = = <br><br>";
print_r($arr4);
echo "</pre>";
输出:
复制代码 代码如下:
Array
(
[a] => Dee
[b] => 2
[0] => 3
[1] => 3
[2] => 5
)
= = = = =
复制代码 代码如下:
Array
(
[a] => Array
(
[0] => 1
[1] => Dee
)
[b] => 2
[0] => 3
[1] => 3
[2] => 5
)
第一个输出的数组中索引为"a"的值1被索引被第二个数组中索引为"a"的值Dee覆盖了。
方法③
复制代码 代码如下:
<?php
function arrsort($arr1,$arr2){