# 8 PHP array interview questions, php array question _ PHP Tutorial

Source: Internet
Author: User
8 PHP array interview questions and php array questions. 8 PHP array interview questions, php array questions online to find the PHP array questions, ready to do it again and record it. 1. create an array with a length of 10 for the write function. the elements in the array are the increasing odd 8 PHP array interview questions and php array questions.

I have prepared to record the PHP array questions on the Internet.

1. write a function to create an array with a length of 10. the elements in the array are an increasing odd number and the first item is 1.
The code is as follows:
<? Php

Function arrsort (\$ first, \$ length ){

\$ Arr = array ();
For (\$ I = \$ first; \$ I <= \$ length; \$ I ++ ){

\$ Arr [] = \$ I * 2-1;
}
Return \$ arr;
}

\$ Arr1 = arrsort (1, 10 );
Print_r (\$ arr1 );

Output:
The code is as follows:
Array ([0] => 1 [1] => 3 [2] => 5 [3] => 7 [4] => 9 [5] => 11 [6] => 13 [7] => 15 [8] => 17 [9] => 19)

2. create an array with a length of 10. the number in the array is an ascending proportional number. the ratio is 3 and the first item is 1.
The code is as follows:
<? Php

// \$ Num is the ratio
Function arrsort (\$ first, \$ length, \$ num ){

\$ Arr = array ();
For (\$ I = \$ first; \$ I <= \$ length; \$ I ++ ){

// Pow (\$ num, \$ I-2); returns the power of \$ num (\$ I-2)
\$ Arr [] = \$ num * pow (\$ num, \$ I-2 );
}
Return \$ arr;
}

\$ Arr1 = arrsort (1, 10, 3 );
Print_r (\$ arr1 );

Output:
The code is as follows:
Array ([0] => 1 [1] => 3 [2] => 9 [3] => 27 [4] => 81 [5] => 243 [6] => 729 [7] => 2187 [8] => 6561 [9] => 19683)

3. calculate the subscript of the largest number in the array.
The code is 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:
The code is as follows:
B

4. create an array with a length of 10. the elements in the array meet the rules of the Fibonacci series.

(The Fibonacci series, also known as the Golden Series, refers to a series of numbers such as 1, 1, 2, 3, 5, 8, 13, 21 ,...... In mathematics, the Fibonacci series are defined as follows by recursive methods: F0 = 0, F1 = 1, Fn = F (n-1) + F (n-2) (n> = 2, n, N *). note: 0th items are 0, and 1st items are the first 1 .)
The code is 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:
The code is as follows:
Array
(
[0] => 0
[1] => 1
[2] => 1
[3] => 2
[4] => 3
[5] => 5
[6] => 8
[7] => 13
[8] => 21
[9] => 34
)

5. calculate the difference between the maximum and minimum numbers in the array.

Two methods:

① Max/min

Output:
The code is as follows:
102

② Sort sorts elements in ascending order/rsort bar elements in ascending order
The code is 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 to directly intercept the last five items of an array with a length greater than 10, without changing the order to the first five items, such as {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} changed to {6, 7, 8, 9, 10, 1, 2, 3, 4, 5 }.

Idea: first extract the corresponding length of the array (array_slice), and then splice two arrays (array_merge)
The code is as follows:
<? Php

Function arrsort (\$ arr ){

\$ Num = count (\$ arr );

If (\$ num> 10 ){

// Array_slice (\$ arr, starting position, intercepting length, retained Index (false by default ))
\$ Arr_firstpart = array_slice (\$ arr, 0, \$ num-5, true );
\$ Arr_lastpart = array_slice (\$ arr, (\$ num-5), 5, true );
} Else {

Echo "the array contains no more than 10 elements. please input it again ";
Exit ();
}

// Splicing
\$ 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"===== After splicing ====";Print_r (arrsort (\$ arr ));Echo"`
";

Output:
The code is 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 splicing ====
The code is 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 length of an array is not equal to 10:
The code is as follows:
\$ Arr = array ("a" => 1, 2, 3 );

Output:
The code is as follows:
Array
(
[A] => 1
[0] => 2
[1] => 3
)

===== After splicing ====

The number of elements in the array cannot exceed 10. enter a new value.

7. connect the two arrays into a new array.

Method ① use the array_merge () function
The code is as follows:
Array_merge (\$ arr1, \$ arr2 );

Method ② recursively append an array using the array_merge_recursive () function

(The array_merge_recursive () function, like the array_merge () function, combines the elements of one or more arrays. the values of an array are appended to the values of the previous array. Returns an array of results.

However, unlike array_merge (), when duplicate key names exist, the value is not overwritten. Instead, multiple values with the same key name are recursively grouped into an array .)
The code is as follows:
<? 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"

`";    print_r(\$arr3);    echo " = = = = = ";    print_r(\$arr4);    echo "`
";

Output:
The code is as follows:
Array
(
[A] => Dee
[B] => 2
[0] => 3
[1] => 3
[2] => 5
)

=====
The code is as follows:
Array
(
[A] => Array
(
[0] => 1
[1] => Dee
)

[B] => 2
[0] => 3
[1] => 3
[2] => 5
)

The value 1 with the index "a" in the first output array is overwritten by the value Dee with the index "a" in the second array.

Method ③
The code is as follows:
<? Php

Function arrsort (\$ arr1, \$ arr2 ){

\$ Arr_new = \$ arr1;

Foreach (\$ arr2 as \$ key => \$ val ){

\$ Arr_new [] = \$ val;
}

Return \$ arr_new;
}

\$ Arr1 = array ("a" => 1, "B" => 2, 3 );
\$ Arr2 = array ("a" => Dee, "c" => 3, 5 );

Echo"

`";print_r(arrsort(\$arr1,\$arr2));echo "`
";

Output:
The code is as follows:
Array
(
[A] => 1
[B] => 2
[0] => 3
[1] => Dee
[2] => 3
[3] => 5
)

If an index array has duplicate indexes, the duplicate index in the second array will be changed to a new index.

8. array reverse order (rsort function cannot be used, and new array cannot be generated)

The array_reverse () function will create a new array, so it cannot be used.
The code is as follows:
<? Php

\$ Arr = array ("a", "B", "c", 1, 10 );
\$ I = ""; // subscript of the number of locations to be replaced
\$ J = ""; // temporary variable
\$ K = ""; // subscript of the number of replaced locations

\$ Len = count (\$ arr );
\$ Half_len = floor (\$ len/2); // round down. The value is the number of cycles.

For (\$ I = 0; \$ I <\$ half_len; \$ I ++ ){

\$ J = \$ arr [\$ I];

// Judge the parity of the number of arrays
If (\$ len % 2! = 0) {// Odd number

\$ 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:
The code is as follows:
Array
(
[0] => 10
[1] => 1
[2] => c
[3] => B
[4] =>
)

I have prepared to record the PHP array questions I found on the Alibaba Cloud website. 1. write a function to create an array with a length of 10. the elements in the array are incrementing odd...

Related Keywords:
Related Article

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.