Home > Net >  PHP array sort using value and create new array
PHP array sort using value and create new array

Time:06-30

Below is my json and I convert json to php array and then try to filter it by months, so that same month data stays in one array , like feb month data under one array , I have treid using foreach to start but not getting exatly how to filter them.

Below is my code:

{

  "exam_dates": [

    {"id":"1","uni_name":"uni1","exam_name":"University of Szeged Feb","exam_date":"02\/02\/2022","exam_mode":"Online","appl_deadline":"To be announced"},
    {"id":"2","uni_name":"uni2","exam_name":"University of Szeged","exam_date":"21\/02\/2022","exam_mode":"Online","appl_deadline":"To be announced"},
    {"id":"3","uni_name":"uni3","exam_name":"University of Szeged","exam_date":"22\/02\/2022","exam_mode":"Online","appl_deadline":"To be announced"},
    {"id":"4","uni_name":"uni4","exam_name":"University of Szeged","exam_date":"32\/03\/2022","exam_mode":"Online","appl_deadline":"To be announced"}

  ]

}

$data = json_decode($body);

Required Output using its key exam_date , so using month filter the data

Array
(
    [0] => Array
        (
            [February] => stdClass Object
                (

                [0] => Array
                    [id] => 1
                    [uni_name] => University of Szeged Feb
                    [exam_name] => University of Szeged Feb
                    [exam_date] => 02/08/2022
                    [exam_mode] => Online
                    [appl_deadline] => To be announced
                )

                [1] => Array
                    [id] => 1
                    [uni_name] => University of Szeged Feb
                    [exam_name] => University of Szeged Feb
                    [exam_date] => 02/08/2022
                    [exam_mode] => Online
                    [appl_deadline] => To be announced
               )

                 [2] => Array
                        [id] => 1
                        [uni_name] => University of Szeged Feb
                        [exam_name] => University of Szeged Feb
                        [exam_date] => 02/08/2022
                        [exam_mode] => Online
                        [appl_deadline] => To be announced
                )

        )

    [1] => Array
        (
            [March] => stdClass Object
                [0] => Array
                    (
                        [id] => 2
                        [uni_name] => University of Szeged
                        [exam_name] => University of Szeged
                        [exam_date] => 03/06/2022
                        [exam_mode] => Online
                        [appl_deadline] => To be announced
                    )
            )
        )

)

I tried using this but not close yet:

foreach ($data->exam_dates as $edates) {

    $month_name =  date("F", strtotime($edates->exam_date));

    $month_key[][$month_name] = $edates;

    echo "<pre>";
    print_r($month_key);

}

CodePudding user response:

You were really close. Basically all you needed to do was change this line

$month_key[][$month_name] = $edates;

To

$month_key[$month_name][] = $edates;

But it would be better to also use a more reliable way of date manipulation so I used the builtin DateTime class instead of strtodate() and date()

$body = '{

    "exam_dates": [
  
      {"id":"1","uni_name":"uni1","exam_name":"University of Szeged Feb","exam_date":"02\/02\/2022","exam_mode":"Online","appl_deadline":"To be announced"},
      {"id":"2","uni_name":"uni2","exam_name":"University of Szeged","exam_date":"21\/02\/2022","exam_mode":"Online","appl_deadline":"To be announced"},
      {"id":"3","uni_name":"uni3","exam_name":"University of Szeged","exam_date":"22\/02\/2022","exam_mode":"Online","appl_deadline":"To be announced"},
      {"id":"4","uni_name":"uni4","exam_name":"University of Szeged","exam_date":"22\/03\/2022","exam_mode":"Online","appl_deadline":"To be announced"}
  
    ]
  
  }';
  
$data = json_decode($body);
#print_r($data);

foreach ($data->exam_dates as $edates) {

    $d = (new DateTime())->CreateFromFormat('d/m/Y',$edates->exam_date);
    // get month name
    $month_name = $d->format('F');
    //format date to USA format
    $edates->exam_date = $d->format('m/d/Y');

    $month_key[$month_name][] = $edates;
}
print_r($month_key);

RESULT

Array
(
    [February] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 1
                    [uni_name] => uni1
                    [exam_name] => University of Szeged Feb
                    [exam_date] => 02/02/2022
                    [exam_mode] => Online
                    [appl_deadline] => To be announced
                )

            [1] => stdClass Object
                (
                    [id] => 2
                    [uni_name] => uni2
                    [exam_name] => University of Szeged
                    [exam_date] => 02/21/2022
                    [exam_mode] => Online
                    [appl_deadline] => To be announced
                )

            [2] => stdClass Object
                (
                    [id] => 3
                    [uni_name] => uni3
                    [exam_name] => University of Szeged
                    [exam_date] => 02/22/2022
                    [exam_mode] => Online
                    [appl_deadline] => To be announced
                )

        )

    [March] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 4
                    [uni_name] => uni4
                    [exam_name] => University of Szeged
                    [exam_date] => 03/22/2022
                    [exam_mode] => Online
                    [appl_deadline] => To be announced
                )

        )

)

CodePudding user response:

Grouping by months alone without taking the year into account doesn't make much sense. That's why I took the year with me into the key. In addition, the date format is poorly chosen.

$groupedArray = [];
foreach($data->exam_dates as $dates){
  $key = dateTime::createFromFormat('!d/m/Y',$dates->exam_date)->format('F Y');
  $groupedArray[$key][] = $dates;
}

$groupedArray =

array (
  'February 2022' => 
  array (
    0 => 
    (object) array(
       'id' => "1",
       'uni_name' => "uni1",
       // :
  • Related