Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Group array php and count

$data = [
     [
       id: '1',
       date_created: '2021-11-14T23:22:53.558225+00:00',
     ],
     [
       id: '2',
       date_created: '2021-11-14T23:22:00.558225+00:00',
     ],
     [
       id: '3',
       date_created: '2021-11-15T11:22:53.558225+00:00',
     ],
]

Help to get a new array, you need to group by date and count the number of resulting arrays starting from the first day.Here is an example of a new array

$new_array = [0,0,0,0,0,0,0,0,0,0,0,0,0,2,1]

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I suppose it can be done with a loop

<?php 
$data = [
     [
       'id' => '1',
       'date_created' => '2021-11-14T23:22:53.558225+00:00',
     ],
     [
       'id' => '2',
       'date_created' => '2021-11-14T23:22:00.558225+00:00',
     ],
     [
       'id' => '3',
       'date_created' => '2021-11-15T11:22:53.558225+00:00',
     ],
];

$res = array();
foreach($data as $row) {
    if(!isset($res[gmdate('d', strtotime($row['date_created']))]))
        $res[gmdate('d', strtotime($row['date_created']))] = 0;
    // array index, (current date of month) gmdate('d', strtotime($row['date_created'])) 
    $res[gmdate('d', strtotime($row['date_created']))]++;
}
print_r($res);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading