How to perform internal operations on 2D array in PHP
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.
- 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);
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 ) )
- 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);
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 ) )
- 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);
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 ) )
- 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;
The running result is:
45
- 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;
The running result is:
5
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8's JIT compilation enhances performance by compiling frequently executed code into machine code, benefiting applications with heavy computations and reducing execution times.

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses symmetric and asymmetric encryption in PHP, comparing their suitability, performance, and security differences. Symmetric encryption is faster and suited for bulk data, while asymmetric is used for secure key exchange.

The article discusses implementing robust authentication and authorization in PHP to prevent unauthorized access, detailing best practices and recommending security-enhancing tools.

Article discusses retrieving data from databases using PHP, covering steps, security measures, optimization techniques, and common errors with solutions.Character count: 159

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses strategies to prevent CSRF attacks in PHP, including using CSRF tokens, Same-Site cookies, and proper session management.
