我想检查数据是否相同,然后不打印数据,而是将数据推送到第一个数据。
例如我有这样的数据数组:
0 => array:4 [▼
"StartTime" => "00:00:00"
"type" => 1
"DateAppointment" => "2019-02-24"
"SDay" => "Sun"
]
1 => array:4 [▼
"StartTime" => "00:00:00"
"type" => 1
"DateAppointment" => "2019-02-25"
"SDay" => "Mon"
]
2 => array:4 [▼
"StartTime" => "00:00:00"
"type" => 1
"DateAppointment" => "2019-02-26"
"SDay" => "Tue"
]
3 => array:4 [▼
"StartTime" => "00:10:00"
"type" => 1
"DateAppointment" => "2019-02-24"
"SDay" => "Sun"
]
数据array
0、1、2具有相同的开始时间,所以我希望我的数据像这样
0 => array:10 [▼
"StartTime" => "00:00:00"
"type" => 1
"DateAppointment" => "2019-02-24"
"SDay" => "Sun"
"type1" => 1
"DateAppointment1" => "2019-02-25"
"SDay1" => "Mon"
"type2" => 1
"DateAppointment2" => "2019-02-26"
"SDay2" => "Tue"
]
1 => array:4 [▼
"StartTime" => "00:10:00"
"type" => 1
"DateAppointment" => "2019-02-24"
"SDay" => "Sun"
]
有可能吗?
我的脚本是这样的
$interval = 10;
$hourMinInterval = 60 - $interval;
for ($i = 0; $i <= 23; $i++){
for ($j = 0; $j <= $hourMinInterval; $j+=$interval){
$h = sprintf('%02d',$i);
$m = sprintf('%02d',$j);
$clock[] = $h.':'.$m.':00';
if ($lastdate != 'lastsunday') {
$timestampa = $lastdate;
} else {
$timestampa = strtotime('last Sunday');
}
for($ble=0;$ble<7;$ble++){
$timeC = sprintf('%02d',$i).':'.sprintf('%02d',$j).':00';
$tgla = strftime('%Y_%m_%d', $timestampa);
$dateS = str_replace('_','-',$tgla);
$sday = strftime('%a', $timestampa);
$timeCs = array('StartTime'=>$timeC, 'type'=>1, 'DateAppointment'=>$dateS, 'SDay'=>$sday);
$timestampa = strtotime('+1 day', $timestampa);
if ($timeC == $timeCs['StartTime']) {
$statusssss[] = $timeCs;
}else{
$statusssss[] = '';
}
}
}
}
如果您有其他方法或使用jsfiddle的任何示例,将不胜感激。
答案 0 :(得分:2)
尝试此代码
$arrayItems = [
[
"StartTime" => "00:00:00",
"type" => 1,
"DateAppointment" => "2019-02-24",
"SDay" => "Sun"
],
[
"StartTime" => "00:00:00",
"type" => 1,
"DateAppointment" => "2019-02-25",
"SDay" => "Mon",
],
[
"StartTime" => "00:00:00",
"type" => 1,
"DateAppointment" => "2019-02-26",
"SDay" => "Tue"
],
[
"StartTime" => "00:10:00",
"type" => 1,
"DateAppointment" => "2019-02-24",
"SDay" => "Sun"
]
];
$mergedArray = [];
foreach($arrayItems as $arrayItem) {
$startTime = $arrayItem['StartTime'];
unset($arrayItem['StartTime']);
$mergedArray[$startTime][] = $arrayItem;
}
echo '<pre>';
var_dump($mergedArray);
结果:
array(2) {
["00:00:00"]=>
array(3) {
[0]=>
array(3) {
["type"]=>
int(1)
["DateAppointment"]=>
string(10) "2019-02-24"
["SDay"]=>
string(3) "Sun"
}
[1]=>
array(3) {
["type"]=>
int(1)
["DateAppointment"]=>
string(10) "2019-02-25"
["SDay"]=>
string(3) "Mon"
}
[2]=>
array(3) {
["type"]=>
int(1)
["DateAppointment"]=>
string(10) "2019-02-26"
["SDay"]=>
string(3) "Tue"
}
}
["00:10:00"]=>
array(1) {
[0]=>
array(3) {
["type"]=>
int(1)
["DateAppointment"]=>
string(10) "2019-02-24"
["SDay"]=>
string(3) "Sun"
}
}
}
答案 1 :(得分:0)
这会给你想要的结果!
$arrayItems = [
[
"StartTime" => "00:00:00",
"type" => 1,
"DateAppointment" => "2019-02-24",
"SDay" => "Sun"
],
[
"StartTime" => "00:00:00",
"type" => 1,
"DateAppointment" => "2019-02-25",
"SDay" => "Mon",
],
[
"StartTime" => "00:00:00",
"type" => 1,
"DateAppointment" => "2019-02-26",
"SDay" => "Tue"
],
[
"StartTime" => "00:10:00",
"type" => 1,
"DateAppointment" => "2019-02-24",
"SDay" => "Sun"
]
];
$mergedArray = [];
$i = 0;
foreach($arrayItems as $arrayItem) {
$keys = array_keys($arrayItem);
foreach($keys as $key){
if(array_key_exists($key, $mergedArray[$arrayItem["StartTime"]])){
$newkey = $key.$i;
$mergedArray[$arrayItem["StartTime"]][$newkey] = $arrayItem[$key];
}
else{
$mergedArray[$arrayItem["StartTime"]][$key] = $arrayItem[$key];
}
}
$i++;
}
echo '<pre>';
var_dump($mergedArray);