How to Efficiently Find Minimum and Maximum Values in a Nested PHP Array?

Barbara Streisand
Release: 2024-10-26 15:04:02
Original
398 people have browsed it

How to Efficiently Find Minimum and Maximum Values in a Nested PHP Array?

Retrieve Minimum and Maximum Values from PHP Array

Problem:

Given a nested PHP array, determine how to efficiently extract the minimum and maximum values for a specific key.

Example:

<code class="php">$array = [
    [
        'id' => '20110209172713',
        'Date' => '2011-02-09',
        'Weight' => '200',
    ],
    [
        'id' => '20110209172747',
        'Date' => '2011-02-09',
        'Weight' => '180',
    ],
    [
        'id' => '20110209172827',
        'Date' => '2011-02-09',
        'Weight' => '175',
    ],
    [
        'id' => '20110211204433',
        'Date' => '2011-02-11',
        'Weight' => '195',
    ],
];</code>
Copy after login

Solution:

Option 1: Using array_column() and min()/max()

This method leverages the array_column() function to extract the 'Weight' values into a numerical array. The min() and max() functions are then used to determine the minimum and maximum values, respectively.

<code class="php">$weights = array_column($array, 'Weight');
$minValue = min($weights);
$maxValue = max($weights);</code>
Copy after login

Option 2: Using array_map()

Similar to Option 1, this method utilizes array_map() to extract the 'Weight' values. However, it requires a custom function to perform the mapping.

<code class="php">$weights = array_map(function($item) {
    return $item['Weight'];
}, $array);

$minValue = min($weights);
$maxValue = max($weights);</code>
Copy after login

Option 3 (deprecated): Using nested loops

This method is not recommended as it is relatively inefficient, especially for large arrays.

<code class="php">$minValue = INF;
$maxValue = -INF;

foreach ($array as $item) {
    if ($item['Weight'] < $minValue) {
        $minValue = $item['Weight'];
    }

    if ($item['Weight'] > $maxValue) {
        $maxValue = $item['Weight'];
    }
}</code>
Copy after login

Option 4: Using array_reduce()

This method is useful for specific scenarios where you need to perform additional calculations or processing while determining the minimum or maximum value.

<code class="php">$minValue = array_reduce($array, function($min, $item) {
    return min($min, $item['Weight']);
}, PHP_INT_MAX);

$maxValue = array_reduce($array, function($max, $item) {
    return max($max, $item['Weight']);
}, -PHP_INT_MAX);</code>
Copy after login

The above is the detailed content of How to Efficiently Find Minimum and Maximum Values in a Nested PHP Array?. 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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!