Max value based on users in a month -- [Question Asked]

Issue

{
    "January": {
        "Alexandria": [
            ["3rd", "7"],
            ["4th", "6"],
            ["5th", "1"]
        ],
        "Bo": [
            ["4th", "13"],
            ["5th", "6"]
        ],
        "Charlie": [
            ["3rd", "12"],
            ["4th", "17"]
        ],
        "James": [
            ["3rd", "3"],
            ["4th", "5"],
            ["5th", "2"]
        ],
        "Joshua": [
            ["3rd", "7"],
            ["4th", "5"],
            ["5th", "2"]
        ],
        "Jacob": [
            ["3rd", "11"],
            ["4th", "12"],
            ["5th", "4"]
        ],
        "Jessey": [
            ["3rd", "13"],
            ["4th", "14"],
            ["5th", "2"]
        ],
        "Mercy": [
            ["3rd", "1"],
            ["4th", "1"]
        ],
        "Olivier": [
            ["3rd", "4"],
            ["4th", "4"],
            ["5th", "4"]
        ],
        "Victor": [
            ["3rd", "1"],
            ["4th", "2"]
        ]
    }
}

I added the max function and it returned the highest number “17” for each developer and respective days.

$months = array();
foreach($best_day as $day) {
    $timestamp = strtotime($day['date']);
    $month = date('F', strtotime($day['date']));
    $date = date('jS', $timestamp);

    $dev = $day['user'];
    $comp = $day['compeleted'];
    $max = '';  
    $make_array[] = $day['compeleted'];
    $max = max($make_array); 

    if (empty($months[$month])) {
        $months[$month] = array();
    }

    $months[$month][$dev][] = [$max];
}

I only want to return the maximum number for each user in a month. For example, Alexandria will only show “3rd”,”7″.

Answer we found from sources

Instead of pushing each date and amount onto a nested array, check if the current amount is greater than the amount there, and replace it if so.

$months = array();
foreach($best_day as $day) {
    $timestamp = strtotime($day['date']);
    $month = date('F', strtotime($day['date']));
    $date = date('jS', $timestamp);

    $dev = $day['user'];
    $comp = $day['compeleted'];
    if (!isset($months[$month][$dev]) || $completed > $months[$dev][$month][$dev][1]) {
        $months[$month][$dev] = [$date, $comp];
    }
}

Answered By – Barmar

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Posted in PHP

Who we are?

We are team of software engineers in multiple domains like Programming and coding, Fundamentals of computer science, Design and architecture, Algorithms and data structures, Information analysis, Debugging software and Testing software. We are working on Systems developer and application developer. We are curious, methodical, rational, analytical, and logical. Some of us are also conventional, meaning we're conscientious and conservative.

Answer collected from stackoverflow and other sources, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0