8 PHP Array interview questions, PHP array questions
Find the PHP array problem on the web, prepare to do it yourself and record it.
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.
Copy the Code code as follows:
<?php
function Arrsort ($first, $length) {
$arr = Array ();
for ($i = $first; $i <= $length; $i + +) {
$arr [] = $i *2-1;
}
return $arr;
}
$arr 1 = arrsort (1,10);
Print_r ($arr 1);
Output:
Copy the Code code as follows:
Array ([0] = 1 [1] = 3 [2] = 5 [3] = 7 [4] = 9 [5] = [6] = [7] = 8 [17] ] = 19)
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.
Copy the Code code as follows:
<?php
$num as a ratio
function Arrsort ($first, $length, $num) {
$arr = Array ();
for ($i = $first; $i <= $length; $i + +) {
Pow ($num, $i-2); return $num ($i-2) Second party
$arr [] = $num *pow ($num, $i-2);
}
return $arr;
}
$arr 1 = arrsort (1,10,3);
Print_r ($arr 1);
Output:
Copy the Code code as follows:
Array ([0] = 1 [1] = 3 [2] = 9 [3] = [4] = Bayi [5] = 243 [6] = 729 [7] = 2187 [8] = = 6561 [9] = 19683)
3. Find the subscript for the largest number in the array.
Copy the Code code as follows:
function Maxkey ($arr) {
$maxval = max ($arr);
foreach ($arr as $key = = $val) {
if ($maxval = = $val) {
$maxkey = $key;
}
}
return $maxkey;
}
$arr = Array (0,-1,-2,5, "B" =>15,3);
echo Maxkey ($arr);
Output:
Copy the Code code as follows:
B
4. Create an array of length 10, and the elements in the array satisfy the law of the Fibonacci sequence.
(Fibonacci series, also known as the Golden Section, refers to a sequence of numbers: 1, 1, 2, 3, 5, 8, 13, 、...... Mathematically, the Fibonacci sequence is defined recursively as follows: F0=0,f1=1,fn=f (n-1) +f (n-2) (n>=2,n∈n*). In particular, the No. 0 item is 0, and the 1th item is the first 1. )
Copy the Code code as follows:
<?php
function Arrfibo ($len) {
$arr [0] = 0;
$arr [1] = 1;
for ($i =2; $i < $len; $i + +) {
$arr [$i] = $arr [$i -1]+ $arr [$i-2];
}
return $arr;
}
echo "
";
Print_r (Arrfibo (10));
echo "
";
Output:
Copy the Code code as follows:
Array
(
[0] = 0
[1] = 1
[2] = 1
[3] = 2
[4] = 3
[5] = 5
[6] = 8
[7] = 13
[8] = 21
[9] = 34
)
5. Calculates the difference between the maximum and minimum numbers in the array.
Two methods:
①max/min
Output:
Copy the Code code as follows:
102
②sort the elements in order from small to large/rsort bar elements sorted from large to small
Copy the Code code as follows:
<?php
function Arrsub ($arr) {
Sort ($arr);
$min = $arr [0];
Rsort ($arr);
$max = $arr [0];
$sub = $max-$min;
return $sub;
}
$arr = Array ( -1,-2,100);
echo arrsub ($arr);
Output:
102
6. 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}.
Idea: first to intercept the corresponding length of the array (Array_slice), and then the 2-segment Array (array_merge)
Copy the Code code as follows:
<?php
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 "
";Print_r ($arr);
echo "
";
Print_r (Arrsort ($arr));
echo "
";
Output:
Copy the Code code as follows:
Array
(
[A] = 1
[0] = 2
[1] = 3
[2] = 8
[3] = 9
[4] = 6
[B] = 5
[5] =-1
[C] = 8
[6] = 0
[7] = 7
)
= = = = = = after stitching = = = =
Copy the Code code as follows:
Array
(
[B] = 5
[0] =-1
[C] = 8
[1] = 0
[2] = 7
[A] = 1
[3] = 2
[4] = 3
[5] = 8
[6] = 9
[7] = 6
)
When the array does not meet the length of 10 o'clock:
Copy the Code code as follows:
$arr = Array ("a" =>1,2,3);
Output:
Copy the Code code as follows:
Array
(
[A] = 1
[0] = 2
[1] = 3
)
= = = = = = after stitching = = = =
The array does not exceed 10 elements, please re-enter
7. Concatenate two arrays into a new array.
Method ① using the Array_merge () function
Copy the Code code as follows:
Array_merge ($arr 1, $arr 2);
Method ② append an array recursively using the array_merge_recursive () function
(The Array_merge_recursive () function, like the Array_merge () function, merges the elements of one or more arrays and appends the values in an array to the previous array. and returns an array as the result.
However, unlike Array_merge (), when there are duplicate key names, the values are not overwritten, but the values of multiple identical key names are recursively grouped into an array. )
Copy the Code code as follows:
<?php
$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 "
";
Print_r ($arr 3); echo "
";
Print_r ($arr 4);
echo "
";
Output:
Copy the Code code as follows:
Array
(
[A] = Dee
[B] = 2
[0] = 3
[1] = 3
[2] = 5
)
= = = = =
Copy the Code code as follows:
Array
(
[a] = = Array
(
[0] = 1
[1] = Dee
)
[B] = 2
[0] = 3
[1] = 3
[2] = 5
)
The value of "a" in the array of the first output is 1 indexed by the value of "a" in the second array, and Dee is covered.
Method ③
Copy the Code code as follows:
<?php
function Arrsort ($arr 1, $arr 2) {
$arr _new = $arr 1;
foreach ($arr 2 as $key = = $val) {
$arr _new[] = $val;
}
return $arr _new;
}
$arr 1 = Array ("A" =>1, "B" =>2,3);
$arr 2 = Array ("A" =>dee, "C" =>3,5);
echo "
";
Print_r (Arrsort ($arr 1, $arr 2));
echo "
";
Output:
Copy the Code code as follows:
Array
(
[A] = 1
[B] = 2
[0] = 3
[1] = Dee
[2] = 3
[3] = 5
)
If it is an indexed array and has a duplicate index, the duplicate index in the second array is modified to the new index.
8. Reverse array (cannot use Rsort function, cannot generate new array)
Using the Array_reverse () function creates a new array, so it cannot be used.
Copy the Code code as follows:
<?php
$arr = Array ("A", "B", "C", 1,10);
$i = "";//subscript to replace the number of positions
$j = "";//Temp variable
$k = "";//subscript of the number of positions to be replaced
$len = count ($arr);
$half _len = Floor ($len/2);//Rounding down, rounding value is the number of cycles
for ($i =0; $i < $half _len; $i + +) {
$j = $arr [$i];
To determine the number of odd pairs of arrays
if ($len%2!=0) {//Odd
$k = $half _len*2-$i;
}else{
Even
$k = $half _len*2-$i-1;
}
$arr [$i] = $arr [$k];
$arr [$k] = $j;
}
echo "
";
Print_r ($arr);
echo "
";
Output:
Copy CodeThe code is as follows:
Array
(
[0] = 10
[1] = 1
[2] = C
[3] = b
[4] = a
)
http://www.bkjia.com/PHPjc/1021095.html www.bkjia.com true http://www.bkjia.com/PHPjc/1021095.html techarticle 8 PHP Array interview questions, PHP array questions on the Internet to find the PHP array problem, ready to do it all over and recorded. 1. Write the function to create an array of length 10, the elements in the array are incrementing odd ...