PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, January 11, 2022

[FIXED] Max value based on users in a month

 January 11, 2022     php     No comments   

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


Solution

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
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing