Home > Backend Development > PHP Problem > How to perform internal operations on 2D array in PHP

How to perform internal operations on 2D array in PHP

PHPz
Release: 2023-04-25 17:46:25
Original
557 people have browsed it

In PHP development, two-dimensional array is a very common data type. Each element in a two-dimensional array is an array and can be manipulated through internal operations. In this article, we will introduce how to perform internal operations on two-dimensional arrays in PHP.

  1. Addition operation

For the addition operation of two-dimensional arrays, we generally use a loop to traverse the array, add the elements at the same position of the two arrays, and then store the result into a new array. The specific code is as follows:

// 定义两个数组
$arr1 = array(array(1,2,3), array(4,5,6), array(7,8,9));
$arr2 = array(array(9,8,7), array(6,5,4), array(3,2,1));

// 定义一个用于存储结果的数组
$result = array();

// 遍历两个数组,并将相同位置的元素相加
for($i=0; $i<count($arr1); $i++) {
    for($j=0; $j<count($arr1[$i]); $j++) {
        $result[$i][$j] = $arr1[$i][$j] + $arr2[$i][$j];
    }
}

// 输出结果
print_r($result);
Copy after login

The running result is:

Array
(
    [0] => Array
        (
            [0] => 10
            [1] => 10
            [2] => 10
        )

    [1] => Array
        (
            [0] => 10
            [1] => 10
            [2] => 10
        )

    [2] => Array
        (
            [0] => 10
            [1] => 10
            [2] => 10
        )

)
Copy after login
  1. Subtraction operation

For the subtraction operation of a two-dimensional array, the array can also be traversed by looping accomplish. The specific code is as follows:

// 定义两个数组
$arr1 = array(array(1,2,3), array(4,5,6), array(7,8,9));
$arr2 = array(array(9,8,7), array(6,5,4), array(3,2,1));

// 定义一个用于存储结果的数组
$result = array();

// 遍历两个数组,并将相同位置的元素相减
for($i=0; $i<count($arr1); $i++) {
    for($j=0; $j<count($arr1[$i]); $j++) {
        $result[$i][$j] = $arr1[$i][$j] - $arr2[$i][$j];
    }
}

// 输出结果
print_r($result);
Copy after login

The running result is:

Array
(
    [0] => Array
        (
            [0] => -8
            [1] => -6
            [2] => -4
        )

    [1] => Array
        (
            [0] => -2
            [1] => 0
            [2] => 2
        )

    [2] => Array
        (
            [0] => 4
            [1] => 6
            [2] => 8
        )

)
Copy after login
  1. Multiplication operation

For the multiplication operation of the two-dimensional array, we also need to traverse the array, And multiply elements at the same position. The specific code is as follows:

// 定义两个数组
$arr1 = array(array(1,2,3), array(4,5,6), array(7,8,9));
$arr2 = array(array(9,8,7), array(6,5,4), array(3,2,1));

// 定义一个用于存储结果的数组
$result = array();

// 遍历两个数组,并将相同位置的元素相乘
for($i=0; $i<count($arr1); $i++) {
    for($j=0; $j<count($arr1[$i]); $j++) {
        $result[$i][$j] = $arr1[$i][$j] * $arr2[$i][$j];
    }
}

// 输出结果
print_r($result);
Copy after login

The running result is:

Array
(
    [0] => Array
        (
            [0] => 9
            [1] => 16
            [2] => 21
        )

    [1] => Array
        (
            [0] => 24
            [1] => 25
            [2] => 24
        )

    [2] => Array
        (
            [0] => 21
            [1] => 16
            [2] => 9
        )

)
Copy after login
  1. Sum operation

For the sum operation of two-dimensional arrays, we can Dimensional arrays are merged into one-dimensional arrays using the array_merge() function, and then the one-dimensional arrays are summed using the array_sum() function. The specific code is as follows:

// 定义一个二维数组
$arr = array(array(1,2,3), array(4,5,6), array(7,8,9));

// 使用array_merge()函数合并成一维数组
$arr1d = array_merge(...$arr);

// 使用array_sum()函数对一维数组进行求和
$sum = array_sum($arr1d);

// 输出结果
echo $sum;
Copy after login

The running result is:

45
Copy after login
  1. Avering value

For the averaging operation of two-dimensional arrays, we can also Use the array_merge() function to merge the two-dimensional arrays into a one-dimensional array, then use the array_sum() function to sum the one-dimensional arrays, and then divide by the length of the array. The specific code is as follows:

// 定义一个二维数组
$arr = array(array(1,2,3), array(4,5,6), array(7,8,9));

// 使用array_merge()函数合并成一维数组
$arr1d = array_merge(...$arr);

// 使用array_sum()函数对一维数组进行求和
$sum = array_sum($arr1d);

// 求数组长度
$count = count($arr1d);

// 求平均值
$avg = $sum / $count;

// 输出结果
echo $avg;
Copy after login

The running result is:

5
Copy after login

Through the above example, we can see that it is very simple to perform internal operations on two-dimensional arrays in PHP. We can use loops to traverse the array to perform operations such as addition, subtraction, multiplication, and division between elements in the array, as well as operations such as summing and averaging. These operations can very conveniently help us process data in two-dimensional arrays.

The above is the detailed content of How to perform internal operations on 2D array in PHP. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template