The mathematical functions of php arrays

There are some math related functions in the php array, most of which start with array and underline with a math English word, as follows:

1 array_diff()
2 array_diff_assoc()
3 array_intersect()
4 array_intersect_assoc()
5 array_sum()
6 array_product()
7 array_count_values()

First is the array set function, the difference set array_diff, which is used to get the different elements of the target array and other arrays

1 $arr = [1, 2, 3, 4, 5, 6, 7, 8];
2 $arr1 = [1, 2, 3, 4, 6];
3 $arr2 = [1, 2, 3, 4, 7];
4 print_r( array_diff( $arr, $arr1, $arr2 ) );
// Array
// (
//     [4] => 5
//     [7] => 8
// )

For associative arrays, it is the same as the above result, because the array [diff] function only compares whether the values are the same

 1 $arr = [ 
 2     'A' => 1, 'B' => 2,
 3     'c1' => 3, 'd1' => 4, 'e1' => 5, 
 4     'f' => 6, 'g' => 7, 'h' => 8
 5 ];
 6 $arr1 = [
 7     'a' => 1, 'b' => 2, 
 8     'c' => 3, 'd' => 4, 'e' => 6
 9 ];
10 $arr2 = [
11     'a' => 1, 'b' => 2,
12     'c' => 3, 'd' => 4, 'e' => 7
13 ];
14 print_r( array_diff( $arr, $arr1, $arr2 ) );
15 // Array
16 // (
17 //     [e1] => 5
18 //     [h] => 8
19 // )

From the name point of view, the array? Diff? Assoc function knows that you need to compare the keys of each element, which is case sensitive,

 1 $arr = [ 
 2     'A' => 1, 'B' => 2,
 3     'c1' => 3, 'd1' => 4, 'e1' => 5, 
 4     'f' => 6, 'g' => 7, 'h' => 8
 5 ];
 6 $arr1 = [
 7     'a' => 1, 'b' => 2, 
 8     'c' => 3, 'd' => 4, 'e' => 6
 9 ];
10 $arr2 = [
11     'a' => 1, 'b' => 2,
12     'c' => 3, 'd' => 4, 'e' => 7
13 ];
14 print_r( array_diff_assoc( $arr, $arr1, $arr2 ) );
15 // Array
16 // (
17 //     [A] => 1
18 //     [B] => 2
19 //     [c1] => 3
20 //     [d1] => 4
21 //     [e1] => 5
22 //     [f] => 6
23 //     [g] => 7
24 //     [h] => 8
25 // )

Array  intersect() function, get the same function as other functions, only compare the teaching value

 1 $arr = [ 
 2     'A' => 1, 'B' => 2,
 3     'c1' => 3, 'd1' => 4, 'e1' => 5, 
 4     'f' => 6, 'g' => 7, 'h' => 8
 5 ];
 6 $arr1 = [
 7     'a' => 1, 'b' => 2, 
 8     'c' => 3, 'd' => 4, 'e' => 6
 9 ];
10 $arr2 = [
11     'a' => 1, 'b' => 2,
12     'c' => 3, 'd' => 4, 'e' => 7
13 ];
14 print_r( array_intersect( $arr, $arr1, $arr2 ) );
15 // Array
16 // (
17 //     [A] => 1
18 //     [B] => 2
19 //     [c1] => 3
20 //     [d1] => 4
21 // )

Array? Intersect? Assoc() only works if the key and value are the same.

 1 $arr = [ 
 2     'a' => 1, 'b' => 2,
 3     'c1' => 3, 'd1' => 4, 'e1' => 5, 
 4     'f' => 6, 'e' => 7, 'h' => 8
 5 ];
 6 $arr1 = [
 7     'a' => 1, 'b' => 2, 
 8     'c' => 3, 'd' => 4, 'e' => 6
 9 ];
10 $arr2 = [
11     'a' => 1, 'b' => 2,
12     'c' => 3, 'd' => 4, 'e' => 7
13 ];
14 print_r( array_intersect_assoc( $arr, $arr1, $arr2 ) );
15 // Array
16 // (
17 //     [a] => 1
18 //     [b] => 2
19 // )

The array sum() function calculates the sum of the values of all elements in the array, and the array product() function calculates the product of all elements in the array,

For element values that are numeric, it's mathematical, but if it's a mix of characters and numbers.

1 $arr = [1, 2, 3, 4, 5, 'a', 'asss'];
2 $arr1 = [ '1', '2', '2as', '2a2' ];
3 echo array_sum($arr), PHP_EOL, array_sum($arr1);
 15
7

This uses the rule of automatic type conversion (string to number). For the array product() function, different mathematical rules lead to different results.

1 $arr = [1, 2, 3, 4, 5, 'a', 'asss'];
2 $arr1 = [ '1', '2', '2as', '2a2' ];
3 echo array_product($arr), PHP_EOL, array_product($arr1);
4 // 0
5 // 8

Array? Count? Values() function, count the number of each value

1 $arr = [1, 2, 3, '1', '2', 1, 2];
2 print_r( array_count_values($arr) );
3 // Array
4 // (
5 //     [1] => 3
6 //     [2] => 3
7 //     [3] => 1
8 // )

From the result, it seems that automatic type conversion rules are also involved. Note: if an array of two dimensions or more is passed into this function, there will be a warning prompt and the value of one dimension array in two dimensions array will not be counted.

Tags: PHP

Posted on Wed, 06 Nov 2019 17:45:39 -0500 by nilesh