i want to delete some index with comparing but it is not easy
first one our php version is 5
for example my result is like this
it is echo by json_encode and it was array
and the data is dynamic
[
{
"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",
}
]
and i want to result like this comparing and unset with the latest one
[
{
"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",
}
]
i tried like this but it is not work
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]);
}
}
}
4
Answers
First, convert your JSON array into a PHP array. then you can remove the array elements using the array index
For delete specific index first you need to convert JSON array into PHP array using json_decode() function, once you convert it into PHP array you can remove specific index using key,
Hope this helps you.
Your variant with summing to columns is not totally correct, because sum of some integers give you wrong result and you can accidentally remove the wrong one. Like
2 + 3 == 1 + 4
not correct.Maybe that code helps you:
You can sort the data by descending order by
care_start_datetime
using usort() andunset
the first index form that sorted data.Code example:
Working demo.