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 關注
- 161 瀏覽
添加回答
舉報