重新映射数组以在php中包含某些值 [英] Remmaping an array to include certain values in php
问题描述
我有以下foreach
$ temp = [];
$ remappedData = [];
$ intCurrentIndex = -1;
foreach(以$ value作为$ value)
{
if(isset($ temp [$ value [$ 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
]
}
]
这按位置对它们进行排序,但我正在尝试
[
{
位置:位置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
]
]
}
]
我尝试过的远不能正常工作。我添加了位置并检查是否已设置,但是它按每个合适的日期对它们进行排序,这不是我所需要的。
根据您的前端,我们可以做到这一点。创建日期数组
我们可以通过以下代码获取日期。
回显日期('D',strtotime('2020-09-18'));
输出:Fri
我们将其作为键并相应地绑定数据及其时间。我们将生成诸如 Fri_date
, Sat_date
之类的键以及诸如 Fri_time
,星期六时间
,您可以直接在每个工作日列中使用。
foreach($结果为$ value)
{
$ strDay = date('D',strtotime($ value ['AptDate'])));
if(isset($ set [$ 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'];
}
}
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'];
}
}
这篇关于重新映射数组以在php中包含某些值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!