1. The write function creates an array of length 10, the elements in the array are incrementing odd numbers, and the first item is 1.
function Createarr ($first, $length) {
for ($i = $first; $i <= $length; $i + +) {
$num = $i-1;
$arr [] = $num;
}
return $arr;
}
Print_r (Createarr (1,10));
2. Create an array of length 10, the number in the array is an increment of the equal number, the ratio is 3, the first item is 1.
function Createarr ($ratio, $length) {
$num = 1/$ratio;
for ($i =1; $i <= $length; $i + +) {
$num = $num * $ratio;
$arr [] = $num;
}
return $arr;
}
Print_r (Createarr (3,10));
3. Find the subscript for the largest number in the array.
function Maxkey ($arr) {
$maxval = max ($arr);
foreach ($arr as $key = = $val) {
if ($maxval = = $val) {
$maxkey = $key;
}
}
return $maxkey;
}
$arr = Array (0,-1,-2,5,15,3);
echo Maxkey ($arr);
4. Create an array of length 10, and the elements in the array satisfy the law of the Fibonacci sequence.
function Arrfibo ($n) {
for ($i =0; $i < $n; $i + +) {
if ($i <2) {
$arr [$i] = 1;
}else{
$arr [$i] = $arr [$i-1] + $arr [$i-2];
}
}
return $arr;
}
Print_r (Arrfibo (8));
5. Write a method that intercepts the last 5 items of an array longer than 10, without changing the order into the first 5 items, such as {1,2,3,4,5,6,7,8,9,10} to {6,7,8,9,10,1,2,3,4,5}.
function Arrsort ($arr) {
$num = count ($arr);
if ($num > 10) {
Array_slice ($arr, starting position, capturing length, preserving index (default = False))
$arr _firstpart = array_slice ($arr, 0, $num -5,true);
$arr _lastpart = Array_slice ($arr, ($num-5), 5,true);
}else{
echo "Array no more than 10 elements, please re-enter";
Exit ();
}
Stitching
$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>= = = = = =" after stitching = = = = <br><br> ";
Print_r (Arrsort ($arr));
echo "</pre>"; 6. Concatenate two arrays into a new array.$arr = Array ("A" =>1, "B" =>2,3);
$arr 2 = Array ("a" =>dee,3,5);
$arr 3 = Array_merge ($arr, $arr 2);
$arr 4 = array_merge_recursive ($arr, $arr 2);
echo "<pre>";
Print_r ($arr 3);
echo "<br> = = = = = <br><br>";
Print_r ($arr 4);
echo "</pre>";
7. Connecting two arrays into a new array
$arr = Array ("A" =>1, "B" =>2,3);
$arr 2 = Array ("A" = "Zou", 3,5);
$arr 3 = Array_merge ($arr, $arr 2);
$arr 4 = array_merge_recursive ($arr, $arr 2);
echo "<pre>";
Print_r ($arr 3);
echo "<br> = = = = = <br><br>";
Print_r ($arr 4);
echo "</pre>";
8. Reverse array (cannot use Rsort function, cannot generate new array)
$arr = Array ("A", "B", "C", 1,10);
$i = "";//subscript to replace the number of positions
$j = "";//temporary variable
$k = "";//subscript of the number of locations to be replaced
$len = count ($arr);
$half _len = Floor ($len/2);//down rounding, the value of rounding is the number of loops
for ($i =0; $i < $half _len; $i + +) {
$j = $arr [$i];
//Judgment array count parity
if ($len%2!=0) {//odd
$k = $half _len*2-$i;
}else{ &NBSP
//even
$K = $half _len*2-$i-1;
}
$arr [$i] = $arr [$k];
$arr [$k] = $j;
}
Echo "<pre>";
Print_r ($arr);
echo "</pre>";
9.PHP Digital amount converted to size format function Daxie ($num) {
$da _num = Array ("0", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine");
$str = ";
if (Is_numeric ($num) && strlen ($num) >0) {
for ($i =0; $i <strlen ($num); $i + +) {
$str. = $da _num[substr ($num, $i, 1)];
}
}
return $str;
}
Echo Daxie (1896);
Arithmetic questions about arrays in PHP