亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何通過比較刪除特定索引

如何通過比較刪除特定索引

PHP
慕娘9325324 2022-01-02 15:59:31
我想通過比較刪除一些索引,但這并不容易第一個我們的 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]);        }    }}
查看完整描述

3 回答

?
慕婉清6462132

TA貢獻1804條經驗 獲得超2個贊

首先,將您的 JSON 數組轉換為 PHP 數組。然后您可以使用數組索引刪除數組元素

$someArray = json_decode($someJSON, true);


查看完整回答
反對 回復 2022-01-02
?
明月笑刀無情

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


查看完整回答
反對 回復 2022-01-02
?
大話西游666

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;

    }

}


查看完整回答
反對 回復 2022-01-02
  • 3 回答
  • 0 關注
  • 161 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號