8 PHP array face test _php tips

Source: Internet
Author: User
Tags pow

Find PHP array questions on the web, prepare to do it yourself and write it down.

1. Write function to create an array with a length of 10, the element in the array is an incremented odd number, and the first entry is 1.

Copy 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 Code code as follows:

Array ([0] => 1 [1] => 3 [2] => 5 [3] => 7 [4] => 9 [5] => [6] => [7] => [8] => 17 [9 ] => 19)

2. Create an array with a length of 10, the number in the array is an incremented equal number, the ratio is 3, and the first is 1.
Copy Code code 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 $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 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 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 Code code as follows:

B

4. Create an array of length 10, in which the elements of the array satisfy the law of the Fibonacci sequence.

The Fibonacci sequence, also called the Golden Section series, refers to a series of 1, 1, 2, 3, 5, 8, 13, 、...... Mathematically, the Fibonacci sequence is defined as the following recursive method: 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 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 "<pre>";
Print_r (Arrfibo (10));
echo "</pre>";

Output:

Copy 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 number and the minimum number in the array.

Two ways:

①max/min

Output:

Copy Code code as follows:

102

②sort to sort the elements from small to large/rsort the elements are sorted from big to small.

Copy 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 to intercept the last 5 items of an array longer than 10, without changing the order into the top 5, as {1,2,3,4,5,6,7,8,9,10} becomes {6,7,8,9,10,1,2,3,4,5}.

Train of thought: first, the corresponding length of the array interception (Array_slice), and then the 2-segment array splicing (Array_merge)

Copy Code code as follows:

<?php

function Arrsort ($arr) {

$num = count ($arr);

if ($num > 10) {

Array_slice ($arr, start position, intercept length, keep index (default is False))
$arr _firstpart = array_slice ($arr, 0, $num -5,true);
$arr _lastpart = Array_slice ($arr, ($num-5), 5,true);
}else{

echo "Array does not exceed 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>";

Output:

Copy 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 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 Code code as follows:

$arr = Array ("a" =>1,2,3);

Output:
Copy Code code as follows:

Array
(
[A] => 1
[0] => 2
[1] => 3
)

= = = = = = after stitching = = = = =

Array does not exceed 10 elements, please re-enter

7. Concatenate two arrays into a new array.

Method ① using the Array_merge () function

Copy Code code as follows:

Array_merge ($arr 1, $arr 2);

method ② Use the array_merge_recursive () function to append the array recursively

(Array_merge_recursive () function, like the Array_merge () function, merges the elements of one or more arrays, and the values in one array are appended to the previous array. and returns the array as the result.

However, unlike Array_merge (), when there is a duplicate key name, the value is not overwritten, but the values of multiple identical key names are recursively grouped into an array. )

Copy 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 "<pre>";
Print_r ($arr 3);

echo "<br> = = = = <br><br>";

Print_r ($arr 4);
echo "</pre>";

Output:

Copy Code code as follows:

Array
(
[A] => Dee
[B] => 2
[0] => 3
[1] => 3
[2] => 5
)

= = = = =
Copy Code code as follows:

Array
(
[A] => Array
(
[0] => 1
[1] => Dee
)

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

The value of "a" in an array of the first output is 1 indexed by the value of "a" in the second array, Dee.

Method ③

Copy 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 "<pre>";
Print_r (Arrsort ($arr 1, $arr 2));
echo "</pre>";

Output:

Copy 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. Array reverse order (cannot use Rsort function, cannot generate new array)

Using the Array_reverse () function creates a new array, so it cannot be used.

Copy Code code as follows:

<?php

$arr = Array ("A", "B", "C", 1,10);
$i = "";//subscript for number of places to replace
$j = "";//Temporary variable
$k = "";//subscript for number of substituted positions

$len = count ($arr);
$half _len = Floor ($len/2);//Rounding down, rounding value is the number of loops

for ($i =0; $i < $half _len; $i + +) {

$j = $arr [$i];

To determine the number of arrays odd and even
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 "<pre>";
Print_r ($arr);
echo "</pre>";


Output:
Copy Code code as follows:

Array
(
[0] => 10
[1] => 1
[2] => C
[3] => b
[4] => a
)

Contact Us

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.