我想通过比较删除一些索引,但这并不容易
第一个我们的php版本是5 例如我的结果是这样的 它由json_encode回显,它是数组 数据是动态的
[
{
"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",
}
]
我想要这样的结果与最新的结果进行比较并取消设置
[
{
"idx": "1",
"mom_member_idx": "1",
"teacher_member_idx": "2",
"care_start_datetime": "2019-09-09 08:30:00",
},
{
"idx": "3",
"mom_member_idx": "2",
"teacher_member_idx": "2",
"care_start_datetime": "2019-09-09 08:30:00",
}
]
我这样尝试过,但是不起作用
while ($r = mysqli_fetch_assoc($chat_list)) {
$dbdata[] = $r;
}
for($r=0;$r<sizeof($dbdata);$r++){
if ($dbdata[$r]['mom_member_idx']+$dbdata[$r]['teacher_member_idx']==$dbdata[$r+1]['mom_member_idx']+$dbdata[$r+1]['teacher_member_idx'])
{
if($dbdata[$r]['care_start_datetime']<$dbdata[$r+1]['care_start_datetime']){
unset($dbdata[$r]);
}else {
unset($dbdata[$r+1]);
}
}
}
答案 0 :(得分:2)
您可以使用usort()和care_start_datetime
排序数据的第一个索引形式按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);
工作demo。
答案 1 :(得分:1)
首先,将JSON数组转换为PHP数组。那么您可以使用数组索引
删除数组元素$ someArray = json_decode($ someJSON,true);
答案 2 :(得分:1)
首先要删除特定索引,您需要使用json_decode()函数将JSON数组转换为PHP数组,将其转换为PHP数组后,您可以使用键删除特定索引,
foreach ($data as $key => $element) {
$value = $element['...'];
if (...) {
unset($data[$key]);
// or
$data[$key]['...'] = '...';
}
}
希望这对您有所帮助。
答案 3 :(得分:1)
您对列求和的变体并不完全正确,因为某些整数的和会给您错误的结果,并且您可能会意外删除错误的结果。像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;
}
}