-1

I have an array with Y-m-d-formatted keys. I want to filter the array so that the only rows kept will be associated with the latest day of a year-month.

Sample array:

array:4 [▼
  "2022-10-30" => array:9 [▶]
  "2022-10-31" => array:6 [▶]
  "2022-11-07" => array:4 [▶]
  "2022-11-09" => array:3 [▶]
]

Desired result:

array:2 [▼
  "2022-10-31" => array:6 [▶]
  "2022-11-09" => array:3 [▶]
]

Important : There may be other months in this array, and not every day of every month is representes in the input array.

I started to write some code, but I'm unsure how to proceed.

public function monthly_average()
{
    
    global $datas,$dates;
    $args= $this->period_monthAverage($datas,$dates);
    dump($args);
    //dump($datas);
    dump($dates);
    dump($dates[0]>$dates[1]);
    $fdate=array();

    for ($i = 0; $i <= sizeof($dates); $i++){
        if(date('m',strtotime($date[i]))==date('m',strtotime($date[i+1]))){

        }
        
       //dump((date('m',strtotime($date)))); 10- 11

    }

}
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Aleyn
  • 35
  • 5
  • "I couldn't build the script" - why not? – Nico Haase Nov 11 '22 at 06:10
  • https://stackoverflow.com/questions/1686724/how-to-find-the-last-day-of-the-month-from-date checkout this answer – Imran Nov 11 '22 at 06:11
  • @Imran that is a different task. – mickmackusa Nov 11 '22 at 06:12
  • I'm not looking for the last day of the month I'm looking for the last day on my own list @Imran – Aleyn Nov 11 '22 at 06:15
  • "find the monthly max date in my list", I searched like this but couldn't find any working code-@mickmackusa – Aleyn Nov 11 '22 at 06:17
  • @NicoHaase "find the monthly max date in my list" can you help ? – Aleyn Nov 11 '22 at 06:22
  • Yeah, I can: the maximum in that list is, as written, "2022-10-31" and "2022-11-09". But I would assume that you want to have this for a dynamic list, containing different dates? Then maybe you should write some code, iterating over the keys, putting them into different subarrays grouped by month, and then getting the maximum value of each of these subarrays? – Nico Haase Nov 11 '22 at 06:29
  • how do i create dynamic array in php? For example, how can I create an instant key for August and September and put all the dates in it? @NicoHaase – Aleyn Nov 11 '22 at 06:32
  • it's not my problem @steven7mwesigwa please read again – Aleyn Nov 11 '22 at 06:33
  • @Aleyn Are the dates from the same year? – steven7mwesigwa Nov 11 '22 at 06:35
  • @Imran please retract your close vote to prevent wasting the time of volunteers who will find this question in the review queue. – mickmackusa Nov 11 '22 at 07:00
  • 1
    @mickmackusa I shared as much as I could – Aleyn Nov 11 '22 at 07:04
  • Sort array by key (New to Old), Group them by Month number, then explode the Array key by month number (-11) the final element at first try is the MAX number and the last element is the MIN. You can try Split and other methos in array map too. – Mehrwarz Nov 11 '22 at 07:09
  • @Meh please use comments to seek clarifications from the asker. You must not provide resolving advice as a comment because that is a blatant disregard for this platform's simple Q&A design. – mickmackusa Nov 11 '22 at 07:13
  • I have rolled back the edit history because your change to the sample data invalidated my and because you absorbed my answer to misrepresent the code that you created on your own. – mickmackusa Nov 11 '22 at 10:53

1 Answers1

0

Maintain a lookup array of year-month keyed values where the values are the latest occurring day of the month on the calendar.

When a later date in the month comes along, replace the elements in the result and update your lookup array.

Code: (Demo)

$result = [];
foreach ($array as $key => $value) {
    $ym = substr($key, 0, 7);
    if (!isset($lookup[$ym]) || $lookup[$ym] < $key) {
        $result[$key] = $value;
        if (isset($lookup[$ym])) {
            unset($result[$lookup[$ym]]);
        }
        $lookup[$ym] = $key;
    }
}
var_export($result);

Or written with fewer function calls: (Demo)

$result = [];
foreach ($array as $key => $value) {
    $ym = substr($key, 0, 7);
    if (!isset($lookup[$ym])) {
        $result[$key] = $value;
        $lookup[$ym] = $key;
    } elseif ($lookup[$ym] < $key) {
        unset($result[$lookup[$ym]]);
        $result[$key] = $value;
        $lookup[$ym] = $key;
    }
}
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • The demos were really instructive, I was able to adapt it to myself, thank you very much, you were very interested – Aleyn Nov 11 '22 at 08:18