【发布时间】: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 好的,我更新了预期的场景
-
一个对象中不能有两个同名的键。在这里你设置了两次
AptDate和AptTime。 -
@JitendraYadav 有没有办法按位置和日期组织数组并有各自的时间?
标签: php arrays reactjs foreach