Skip to content
Advertisement

remapping and grouping repeated values from an array in php

I am trying to remap an response from a query to the database and group like items in one array. for example, from this example below.

Response:

[
    "Location"=> "City 1",
    "AptDate"=> "2020-09-16",
    "AptTime"=> "11:00",
    "AptLength"=> "45",
    "AptStatus"=> "1",
    "Operatory"=> "1 RECALL",
    "OperatoryNum"=> "2"
],    
[
    "Location"=> "City 2",
    "AptDate"=> "2020-09-16",
    "AptTime"=> "09:00",
    "AptLength"=> "45",
    "AptStatus"=> "1",
    "Operatory"=> "1 RECALL",
    "OperatoryNum"=> "2"
],
[
    "Location"=> "City 1",
    "AptDate"=> "2020-09-16",
    "AptTime"=> "12:00",
    "AptLength"-> "45",
    "AptStatus"=>"1",
    "Operatory"=> "1 RECALL",
    "OperatoryNum"=> "2"
[,

looping through results:

           $remappedData=[];
                foreach ($result as $value)
                {
                    $remappedData[] = [
                        'location' => $value['Location'],

                        // And so on
                    ];

                }

}

This doesnt really give me what i need as I am trying to group the array based on Location and add the AppDate base on that location. Something like this.

    {
        "Location": "City 1",
        "AptDate": ["2020-09-16","2020-09-16"],
        "AptTime": ["11:00","12:00"],
        "AptLength": ["45","45"],
        "AptStatus": ["1","1"],
        "Operatory": ["1 RECALL","1 RECALL"],
        "OperatoryNum": ["2","2"]
    },
    {
        "Location": "City 2",
        "AptDate": ["2020-09-16"],
        "AptTime": ["09:00"],
        "AptLength":[ "45"],
        "AptStatus": ["1"],
        "Operatory": ["1 RECALL"],
        "OperatoryNum": "2"
    },

Advertisement

Answer

So based on your scenario, you want grouping on Location and group all other attributes by keeping them in array.

foreach ($result as $value)
{
   $remappedData[$value['Location']]['location'] = $value['Location'];
   $remappedData[$value['Location']]['AptDate'][] = $value['AptDate']; // Notice we're creating an array here.
   $remappedData[$value['Location']]['AptTime'][] = $value['AptTime'];
   // so on all other attributes which needs to be grouped.
}

Note: Here we’ve created index as Location. If we don’t want Location as key, we want as array(may be for frontend) use below code.

$temp = [];
$remappedData=[];
$intCurrentIndex = -1;

foreach ($result as $value)
{
    if(isset($temp[$value['Location']])){
         $oldIndex = $temp[$value['Location']];
         $remappedData[$oldIndex]['AptDate'][] = $value['AptDate'];
         $remappedData[$oldIndex]['AptTime'][] = $value['AptTime'];
    }
    else{
          $temp[$value['Location']] = ++$intCurrentIndex;
          $remappedData[$intCurrentIndex]['location'] = $value['Location'];
          $remappedData[$intCurrentIndex]['AptDate'][] = $value['AptDate'];
          $remappedData[$intCurrentIndex]['AptTime'][] = $value['AptTime'];
    }
  
}
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement