我想要得到的是删除 Rest 字段中的重复值,但我想分配/保留其原始日期。元素:array (size=413) 0 => array (size=5) 'Date' => array (size=1) 0 => int 1588520980 'Rest' => 123abc 1 => array (size=5) 'Date' => array (size=1) 0 => int 1588520981 'Rest' => qwe123 2 => array (size=5) 'Date' => array (size=1) 0 => int 1588520983 'Rest' => qwe123我试过了,但没用public function find_repeats($arr){ foreach(array_column($arr, 'Rest') as $ckey=>$value) { $keys = array_reverse(array_keys(array_column($arr, 'Rest'), $value)); foreach ($keys as $v) { if ($ckey != $v && isset($arr[$v])) { $arr[$ckey]['Date'][] = $arr[$v]['Date'][0]; unset($arr[$v]); } } } return $arr;}这是此操作后表格的外观array (size=413) 0 => array (size=5) 'Date' => array (size=1) 0 => int 1588520980 'Rest' => 123abc 1 => array (size=5) 'Date' => array (size=1) 0 => int 1588520981 1 => int 1588520983 'Rest' => qwe123感谢帮助!:)
1 回答
ITMISS
TA贡献1871条经验 获得超8个赞
没有所有这些堆叠功能的简单解决方案:
$newData = [];
foreach ($arr as $item) {
$rest = $item['Rest'];
if (!isset($newData[$rest])) {
$newData[$rest] = $item;
} else {
$newData[$rest]['Date'][] = $item['Date'][0];
}
}
// optionally apply array_values to get 0-indexed array:
$newData = array_values($newData);
- 1 回答
- 0 关注
- 120 浏览
添加回答
举报
0/150
提交
取消