skip to Main Content

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


  1. First, convert your JSON array into a PHP array. then you can remove the array elements using the array index

    $someArray = json_decode($someJSON, true);

    Login or Signup to reply.
  2. 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,

    foreach ($data as $key => $element) {
        $value = $element['...'];
        if (...) {
            unset($data[$key]);
            // or
            $data[$key]['...'] = '...';
        }
    }
    

    Hope this helps you.

    Login or Signup to reply.
  3. 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:

    $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;
        }
    }
    
    Login or Signup to reply.
  4. You can sort the data by descending order by care_start_datetime using usort() and unset the first index form that sorted data.

    Code example:

    $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);
    

    Working demo.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search