Home  >  Article  >  Backend Development  >  How to Find the Highest "Total" Value and its Corresponding Data in a Multidimensional Array?

How to Find the Highest "Total" Value and its Corresponding Data in a Multidimensional Array?

DDD
DDDOriginal
2024-11-10 06:26:02284browse

How to Find the Highest

Determining the Highest Value in a Multidimensional Array

Finding the highest value within a multidimensional array presents a unique challenge. In this specific case, the array contains elements with a key "Total" that represents a numerical value.

One approach is to create a new array containing only the "Total" values:

$totals = array_column($array, 'Total');

We can then use the max() function to find the maximum value in the new array:

$maxTotal = max($totals);

However, this approach does not provide a direct way to retrieve the corresponding data from the original array.

To overcome this, we can use a nested loop to iterate through the original array and compare the "Total" values:

$maxTotal = 0;
$maxIndex = 0;
foreach ($array as $index => $item) {
    if ($item['Total'] > $maxTotal) {
        $maxTotal = $item['Total'];
        $maxIndex = $index;
    }
}

Once the highest "Total" value is found, we can use the $maxIndex to access the corresponding data in the original array. This approach allows us to retrieve both the maximum value and the associated data efficiently.

The above is the detailed content of How to Find the Highest "Total" Value and its Corresponding Data in a Multidimensional Array?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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