【问题标题】:Remmaping an array to include certain values in php重新映射数组以在 php 中包含某些值
【发布时间】:2020-09-17 20:31:13
【问题描述】:

我有以下 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'];
        }

    }

这会按位置及其 aptDates 和时间对所有数据进行排序。示例:

[
   {
      "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"
      ]
   }
]

这会按位置对它们进行排序,但我试图保持这种格式,但也包括时间和日期。

 [
       {
          "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"
               ]
           ]
       }
    ]

到目前为止我尝试过的方法都不起作用。我添加了位置并检查是否已设置,但它按每个 aptdate 对它们进行排序,这不是我需要的。
value now

试图在 react-table 中显示它,但我收到一条错误消息 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>
  ));
};

尝试将其添加到这样的表中。

【问题讨论】:

  • 我将更新输入数据的问题,请更新预期的输出。
  • 我正在谈论的场景是,如果有多个日期以任何顺序并且相应的时间也没有排序怎么办。您期望的输出是什么?
  • @JitendraYadav 好的,我更新了预期的场景
  • 一个对象中不能有两个同名的键。在这里你设置了两次AptDateAptTime
  • @JitendraYadav 有没有办法按位置和日期组织数组并有各自的时间?

标签: php arrays reactjs foreach


【解决方案1】:

根据您的前端,我们可以做些什么。根据星期一、星期二等日期创建日期和时间数组。

我们可以通过以下代码获得一天。

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

Output: Fri

我们将把它作为键并相应地绑定数据和它的时间。我们将生成Fri_dateSat_date 等键和Fri_timeSat_time 等时间,您可以直接将其用于每个工作日列。

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

}

【讨论】:

  • 看起来日期是一个对象键,因此在前端渲染它很棘手
  • 我在原问题中添加了一张图片。
  • 让我看看。如果是这样,您需要提供预期的输出。所以我们可以解决这个问题
  • 图片包含预期的数据。我认为您的前端还有其他一些限制。直到我们知道,我们如何安排数据来支持前端。我们不知道如何走得更远。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多