Home > Blockchain >  Max value based on users in a month
Max value based on users in a month

Time:01-06

{
    "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".

CodePudding user response:

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];
    }
}
  •  Tags:  
  • Related