简体   繁体   中英

Remmaping an array to include certain values in php

I have the following foreach

$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'];
        }

    }

This sorts all the data by location along with its aptDates and times. Example:

[
   {
      "location":"Location 1",
      "AptDate":[
         "2020-09-17",
         "2020-09-17",
         "2020-09-17",
         "2020-09-17",
         "2020-09-17",
         "2020-09-17",
         "2020-09-17",
         "2020-09-17",
         "2020-09-17",
         "2020-09-17",
         "2020-09-17",
         "2020-09-18",
         "2020-09-18",
         "2020-09-18",
         "2020-09-18",
         "2020-09-18",
         "2020-09-18",
         "2020-09-18",
         "2020-09-18",
         "2020-09-18"
      ],
      "AptTime":[
         "15:15",
         "15:45",
         "15:30",
         "15:30",
         "16:15",
         "15:15",
         "15:45",
         "14:45",
         "15:15",
         "16:00",
         "14:45",
         "08:30",
         "09:30",
         "10:15",
         "12:30",
         "13:30",
         "14:30",
         "15:45",
         "08:00",
         "09:00"
      ]
   }
]

This sorts them by location but i am trying to keep this format but also include the times with the dates.

 [
       {
          "location":"Location 1",
          "AptDate": [
              "2020-09-17" : [
                   "15:15",
                   "15:45",
                   "15:30",
                   "15:30",
                   "16:15",
                   "15:15",
                   "15:45",
                   "14:45",
                   "15:15",
                   "16:00",
               ],
              "2020-09-18" :[
                  "08:30",
                  "09:30",
                  "10:15",
                  "12:30",
                  "13:30",
                  "14:30",
                  "15:45",
                  "08:00",
                  "09:00"
               ]
           ]
       }
    ]

What I have tried so far is not working. I adding the location and checking if is set but it sorts them by each individual aptdate, which is not what I need.
value now

Trying to display this in a react-table but I am getting an error saying Cannot read property 'map' of undefined

const Cell = ({ cell }) => {
  return cell.row.original.AptDate.map((value, index) => (
      <span
          key={index}
          style={{ display: "grid", textAlign:"center", marginRight: index === 0 ? 8 : 0 }}
      >
      {value}
    </span>
  ));
};

Trying to add this to a table like this.

前端表

Based on your frontend, what we can do it. Create an array of dates & times based on day like monday, tuesday and so on.

we can get day by following code.

echo date('D', strtotime('2020-09-18'));

Output: Fri

We will make it as key and bind data and its time accordingly. We will generate keys like Fri_date , Sat_date and times like Fri_time , Sat_time which you can directly use for each week day column.

foreach ($result as $value)
{
    $strDay = date('D', strtotime($value['AptDate']));
    if(isset($temp[$value['Location']])){
        $oldIndex = $temp[$value['Location']];
        $remappedData[$oldIndex][$strDay. '_date'] = $value['AptDate'];
        $remappedData[$oldIndex][$strDay. '_time'][] = $value['AptTime'];
    }
    else{
        $temp[$value['Location']] = ++$intCurrentIndex;
        $remappedData[$intCurrentIndex]['location'] = $value['Location'];
        $remappedData[$intCurrentIndex][$strDay. '_date'] = $value['AptDate'];
        $remappedData[$intCurrentIndex][$strDay. '_time'][] = $value['AptTime'];
   }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM