假设我有一个这样的Object数组
array
0 =>
object(stdClass)
public 'ID' => string '1'
public 'DATE' => string '26/02/2018'
1 =>
object(stdClass)
public 'ID' => string '2'
public 'DATE' => string '27/02/2018'
2 =>
object(stdClass)
public 'ID' => string '1'
public 'DATE' => string '28/02/2018'
3 =>
object(stdClass)
public 'ID' => string '3'
public 'DATE' => string '29/02/2018'
在这里您可以看到有两个ID相同,即ID = 1,但日期不同。我想像这样合并相同的ID及其数据
array
0 =>
object(stdClass)
public 'ID' => string '1'
public 'DATE' => string '26/02/2018, 28/02/2018'
1 =>
object(stdClass)
public 'ID' => string '2'
public 'DA_DATE' => string '26/02/2018'
2 =>
object(stdClass)
public 'ID' => string '3'
public 'DATE' => string '27/02/2018'
如何合并数据,以便针对相同的ID将两个不同的日期合而为一?
答案 0 :(得分:3)
假设您将日期设置为数组:
$ adb device
如果您希望日期是一个用逗号分隔而不是数组的字符串,那么:
$grouped = [];
foreach ($items as $item) {
$grouped[$item->ID]["ID"] = $item->ID;
$grouped[$item->ID]["DATE"][] = $item->DATE;
}
$grouped = array_values($grouped);