Pratik Patel
Pratik Patel

Reputation: 131

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
                            )
                    )

        )

)

Upvotes: 0

Views: 48

Answers (2)

Siddhartha Chowdhury
Siddhartha Chowdhury

Reputation: 2732

Say the array is question is $arr

This will arrange the array in the way you wanted as solution

foreach ($arr as $key => $value) {
    if(!is_array($value['pc_eventDate]))
          $value['pc_eventDate] = [];
    if(!is_array($value['pc_eventDate]['name'])) 
          $value['pc_eventDate]['name'] = []; 
    if(!is_array($value['pc_eventDate']['name']['ufname'.' ulname'])){
          $value['pc_eventDate']['name']['ufname'.' ulname'] = [];
           $value['pc_eventDate']['name']['ufname'.' ulname']['appointments'] = 1;
    }else{
         $value['pc_eventDate']['name']['ufname'.' ulname']['appointments'] += 1;
    }
}

You need to do something like the above. Try running the above code, there could be some typo. If its doesnt yields your desired result, try commenting out all the lines in the body of the foreach and var_dump() each step to test the building of the array structure.

Thanks

Upvotes: 0

derelict
derelict

Reputation: 2082

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!

Upvotes: 1

Related Questions