PHP Program to Find the Maximum Element in a Matrix
Last Updated :
19 Mar, 2024
Given a matrix, the task is to find the maximum element of the matrix (arrays of arrays) in PHP. Finding the maximum element in a matrix is a common task that involves iterating through all the elements of the matrix and keeping track of the largest value found.
Example:
Input: matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
Output: Maximum Element: 9
These are the following approaches:
Approach 1: Using Nested Loops
The basic method to find the maximum element in a matrix is by using nested loops to iterate through each element. PHP_INT_MIN is used to initialize the $max variable to the smallest possible integer value in PHP. The nested foreach loops iterate through each element of the matrix. Inside the inner loop, the $max variable is updated whenever a larger element is found.
Example: This example shows the use of nested loop for finding out the maximum element.
PHP
<?php
function findMaxElement($matrix) {
// Initialize max to the
// smallest possible value
$max = PHP_INT_MIN;
// Iterate through each row
// of the matrix
foreach ($matrix as $row) {
// Iterate through each
// element of the row
foreach ($row as $element) {
// Update max if a larger
// element is found
if ($element > $max) {
$max = $element;
}
}
}
return $max;
}
// Driver Code
// Given matrix
$matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
$max = findMaxElement($matrix);
// Display the maximum element
echo "Maximum Element in the matrix: $max";
?>
OutputMaximum Element in the matrix: 9
Approach 2: Using array_reduce() and max() Functions
Another method to find the maximum element in a matrix is by using the array_reduce() function in combination with the max() function. The array_reduce() is used to iteratively reduce the matrix to a single value, which is the maximum element. The max() function is used twice, once to find the maximum element in each row, and once to compare that value with the current maximum ($carry). PHP_INT_MIN is used as the initial value for $carry to ensure that any element in the matrix will be larger.
Example: This example shows the use of array_reduce() method to find out the max element.
PHP
<?php
function findMaxElement($matrix) {
// Use array_reduce() function to
// find the maximum element
$max = array_reduce($matrix, function ($carry, $row) {
return max($carry, max($row));
}, PHP_INT_MIN);
return $max;
}
// Given matrix
$matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
$max = findMaxElement($matrix);
// Display the maximum element
echo "Maximum Element in the matrix: $max";
?>
OutputMaximum Element in the matrix: 9
Approach 3: Using array_map() and max() Functions
Alternatively, you can use array_map() to apply the max() function to each row of the matrix and then use max() again to find the overall maximum. The array_map('max', $matrix) applies the max() function to each row of the matrix, resulting in an array of maximum values for each row. The max(...) is then used again to find the maximum value in this array of maximum row values.
Example: This example shows the use of array_map() method to find out the max element.
PHP
<?php
function findMaxElement($matrix) {
// Use array_map() function to apply
// max to each row, then find the
// max of those values
$max = max(array_map('max', $matrix));
return $max;
}
// Driver Code
// Given matrix
$matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
$max = findMaxElement($matrix);
// Display the maximum element
echo "Maximum Element in the matrix: $max";
?>
OutputMaximum Element in the matrix: 9
Similar Reads
PHP program to find the maximum and the minimum in array Finding the maximum and minimum in an array involves determining the largest and smallest values within a given set of numbers. This task is crucial for analyzing data, identifying trends, or filtering outliers. Various methods, from simple traversal to optimized algorithms, can achieve this.Example
7 min read
Find Column with Maximum Zeros in Matrix Given a matrix(2D array) M of size N*N consisting of 0s and 1s only. The task is to find the column with the maximum number of 0s. If more than one column exists, print the one which comes first. If the maximum number of 0s is 0 then return -1. Examples: Input: N = 3, M[][] = {{0, 0, 0}, {1, 0, 1},
5 min read
Find the Second Largest Element in an Array in PHP We will be given with an integer array, i.e. $array1 or $ array2, and the task is to find the second largest element in the array. There are multiple ways to approach and solve this problem however using sorting is the most common and concise approach as we use the rsort() function for further opera
3 min read
Find the Most Frequent Element in an Array in PHP Given an array, i.e. $arr, the task is to find the most frequent element in an array. There are multiple ways to solve this problem in PHP we will be going to discuss them. These problems can be used during data analysis, Web Analytics, or highly used in fraud detection. Example: Input: $array = [1,
2 min read
How to Access and Print Matrix Element at Specific Position in PHP ? Accessing and printing elements at specific positions within a matrix is a fundamental operation in PHP when dealing with two-dimensional arrays. A matrix in PHP can be represented as an array of arrays, where each sub-array represents a row in the matrix. This article explores various approaches to
3 min read
Find Indices of Maximum and Minimum Value of Matrix in MATLAB Matrices in MATLAB are 2-dimensional arrays that store mostly numeric data at different indices. Now, to find the indices of maximum and minimum values of a given matrix, MATLAB does not provide any direct functionality however, we can do the same by using two other functionalities. Firstly, we will
3 min read
How to Calculate Determinant of a Matrix in PHP? Given a Matrix of size M*N, where N and M is an Integer, we need to Calculate calculate of a Matrix in PHP. Example: Input: [[1, 2],[2, 3]]Output: 6Explanation: (1*3)-(2*2)= 6-4= 2 There are several ways to find the determinant of the matrix in PHP which are as follows: Table of Content Using Recurs
4 min read
How to Declare and Initialize 3x3 Matrix in PHP ? PHP, a widely used scripting language, is primarily known for its web development capabilities. However, it also offers a robust set of features to perform various other programming tasks, including the creation and manipulation of matrices. A matrix is a two-dimensional array consisting of rows and
3 min read
Find a Specific Pair in Matrix using JavaScript In this problem, we are given a 2-D matrix and we have to find the maximum value of âMAT[a][b]â - âMAT[c][d]â over all possible indices (0 <= âaâ, âbâ, âcâ, âdâ < âNâ) such that . âaâ > âcâ and âbâ > âdâ. Below is an example to understand the problem statement clearly Example: Input [ [1
3 min read
PHP max() Function The max() function is an inbuilt function in PHP, used to find the numerically maximum value in an array or the numerically maximum value of several specified values. Syntaxmax(array_values) or max(value1, value2, ...)ParametersThis function accepts two different types of arguments which are explain
2 min read