3 回答

TA贡献1804条经验 获得超2个赞
首先,将您的 JSON 数组转换为 PHP 数组。然后您可以使用数组索引删除数组元素
$someArray = json_decode($someJSON, true);

TA贡献1828条经验 获得超4个赞
您可以care_start_datetime使用usort()和unset对数据进行排序的第一个索引形式按降序对数据进行排序。
代码示例:
$data = json_decode('[
{"idx": "1","mom_member_idx": "1","teacher_member_idx": "2","care_start_datetime": "2019-09-09 08:30:00"},
{"idx": "2","mom_member_idx": "1","teacher_member_idx": "2","care_start_datetime": "2019-09-10 08:30:00"},
{"idx": "3","mom_member_idx": "2","teacher_member_idx": "2","care_start_datetime": "2019-09-09 08:30:00"}
]', true);
usort($data, function($a, $b) {
return strtotime($a['care_start_datetime']) > strtotime($b['care_start_datetime']) ? -1 : 1;
});
unset($data[0]);
print_r($data);

TA贡献1817条经验 获得超14个赞
您对列求和的变体并不完全正确,因为某些整数的总和会给您错误的结果,您可能会意外删除错误的结果。喜欢2 + 3 == 1 + 4不正确。
也许该代码可以帮助您:
$groupedData = array();
while ($r = mysqli_fetch_assoc($chat_list)) {
// We create unique key for group same rows
// (with the same 'mom_member_idx' and 'teacher_member_idx')
$uniqueKey = implode('_', [$r['mom_member_idx'], $r['teacher_member_idx']]);
if (!array_key_exists($uniqueKey, $groupedData)) {
$groupedData[$uniqueKey] = array();
}
// Add row to grouped array by our unique key
$groupedData[$uniqueKey][] = $row;
}
$result = array();
foreach ($groupedData as $rows) {
// If in grouped array with that unique key only one row - just add it
if (count($rows) <= 1) {
$result[] = $rows[0];
continue;
}
// Go for all same rows and check date
$neededRow = null;
foreach ($rows as $row) {
// Here I don't understand what kind of date do you want - minimum or maximum
// You just can reverse the sign
if (
is_null($neededRow) ||
strtotime($row['care_start_datetime']) > strtotime($neededRow['care_start_datetime'])
) {
$neededRow = $row
}
}
if ($neededRow) {
$result[] = $neededRow;
}
}
- 3 回答
- 0 关注
- 165 浏览
添加回答
举报