Search code examples
phparrayssortingmultidimensional-arrayarray-merge

Manipulation or modification of array


I'm new to PHP. I'm doing one my project with php and I'm new to array functions and all those things. I have tried but not get success in that. let me show you my sql query array. I have one my array which is as below:

Array
(
    [0] => Array
        (
            [pc_eventDate] => 2016-08-25
            [ufname] => Rutul
            [ulname] => Shah
            [name] =>  Clinic
        )

    [1] => Array
        (
            [pc_eventDate] => 2016-08-26
            [ufname] => Rutul
            [ulname] => Shah
            [name] =>  Clinic
        )

    [2] => Array
        (
            [pc_eventDate] => 2016-08-25
            [ufname] => Administrator
            [ulname] => Administrator
            [name] =>  Clinic
        )

    [3] => Array
        (
            [pc_eventDate] => 2016-08-26
            [ufname] => Administrator
            [ulname] => Administrator
            [name] =>  Clinic
        )

    [4] => Array
        (
            [pc_eventDate] => 2016-08-25
            [ufname] => Administrator
            [ulname] => Administrator
            [name] =>  Clinic
        )

    [5] => Array
        (
            [pc_eventDate] => 2016-08-26
            [ufname] => Amit
            [ulname] => Mahida
            [name] => Cancer Specialist
        )

    [6] => Array
        (
            [pc_eventDate] => 2016-08-26
            [ufname] => Amit
            [ulname] => Mahida
            [name] => Breach Candy Hospital
        )

)

Now I want my resulted array as below :

Array
(
    [2016-08-25] => Array
        (
            [ Clinic] => Array
                (
                    [Rutul Shah] => Array
                        (
                            [appointments] => 1
                        )

                    [Administrator Administrator] => Array
                        (
                            [appointments] => 2
                        )

                )

        )

    [2016-08-26] => Array
        (
            [Clinic] => Array
                (
                    [Rutul Shah] => Array
                        (
                            [appointments] => 1
                        )
                    [Administrator Administrator] => Array
                        (
                            [appointments] => 1
                        )

                )
                [Cancer Specialist] => Array
                    (
                        [Amit Mahida] => Array
                            (
                                [appointments] => 1
                            )
                    )
                [Breach Candy Hospital] => Array
                    (
                        [Amit Mahida] => Array
                            (
                                [appointments] => 1
                            )
                    )

        )

)

Solution

  • you want to loop through your appointments array and use its contents to generate the other data structure. let's call your first array $input and your second array $output:

    // initialize output array
    $output = [];
    
    // loop through each $appt in the $input array
    foreach($input as $appt) {
      // get shorter var names for appt data
      $date = $appt['pc_eventDate'];
      $name = $appt['name'];
      $uname = $appt['ufname'].' '.$appt['ulname'];
    
      // initialize each level of the data structure if it doesn't already exist
      if(!isset($output[$date])) $output[$date] = [];   
      if(!isset($output[$date][$name])) $output[$date][$name] = [];
      if(!isset($output[$date][$name][$uname])) $output[$date][$name][$uname] = [];
      // initialize the number of appts to 0
      if(!isset($output[$date][$name][$uname]['appointments'])) $output[$date][$name][$uname]['appointments'] = 0;
    
      // increment the number of appts
      $output[$date][$name][$uname]['appointments']++;
    }
    

    the important thing is the intialization of each sub-array in the new structure according to the data in the old structure -- from there we're just counting the number of appointments that match the new data.

    good luck!