第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

重新排列數(shù)組數(shù)據(jù)并在新數(shù)組 php 中分開

重新排列數(shù)組數(shù)據(jù)并在新數(shù)組 php 中分開

PHP
慕田峪4524236 2023-07-08 16:39:31
數(shù)組aArray(    [0] => Array        (            [member_id] => 6            [sorting] => 0            [total] => 1        )    [1] => Array        (            [member_id] => 7            [sorting] => 1            [total] => 2        ))數(shù)組bArray(    [0] => Array        (            [member_id] => 2            [total] => 3        )    [1] => Array        (            [member_id] => 6            [total] => 1        )    [2] => Array        (            [member_id] => 7            [total] => 2        ))問題:以上數(shù)據(jù)我嘗試循環(huán)數(shù)組并存儲到新數(shù)組中,如下所示(最終數(shù)據(jù))。首先,如果第二個數(shù)組缺少成員數(shù)據(jù),那么我需要從第一個數(shù)組中獲取成員數(shù)據(jù)并將其存儲到新數(shù)組中。除此之外,如果缺少排序,我需要從第一個數(shù)組中獲取排序值。最后,我能夠很好地獲取所有數(shù)據(jù),但最后存儲到其中的值將會重復(fù)。任何人都可以幫忙:(?我的邏輯停留在這里。我的代碼返回Array(    [0] => Array        (            [member_id] => 6            [total] => 1            [sorting] => 0        )    [1] => Array        (            [member_id] => 7            [total] => 2            [sorting] => 1        )    [2] => Array        (            [member_id] => 7            [total] => 2            [sorting] =>         ))最終數(shù)據(jù)應(yīng)該是:Array(    [0] => Array        (            [member_id] => 6            [total] => 1            [sorting] => 0        )    [1] => Array        (            [member_id] => 7            [total] => 2            [sorting] => 1        )    [2] => Array        (            [member_id] => 2            [total] => 3            [sorting] => 2        ))代碼: https: //3v4l.org/WBLCC
查看完整描述

2 回答

?
慕娘9325324

TA貢獻1783條經(jīng)驗 獲得超4個贊

// create a version of $a that uses the member_id as key, makes look-up easier

$a_reindexed = array_combine( array_column($a, 'member_id'), array_values($a) );

// get the current maximum sorting value from the elements in $a

$max_sort = max(array_column($a, 'sorting'));


$result = [];

// add values from $b to the result

foreach($b as $key => $value) {

    $result[$key] = $value;

    // set the sorting property to that of the corresponding element from $a_reindexed

    // or PHP_INT_MAX if the former doesn’t exist

    $result[$key]['sorting'] = $a_reindexed[$value['member_id']]['sorting'] ?? PHP_INT_MAX;

}

// sort the result array by the sorting value, so all elements with PHP_INT_MAX will go to the end

usort($result, function($a, $b) { return $a['sorting'] <=> $b['sorting']; });

// set new sorting value for all items having it set to PHP_INT_MAX

foreach($result as $key => $value) {

  if($value['sorting'] == PHP_INT_MAX) {

    $result[$key]['sorting'] = ++$max_sort;

  }

}


查看完整回答
反對 回復(fù) 2023-07-08
?
夢里花落0921

TA貢獻1772條經(jīng)驗 獲得超6個贊

可能適合您需求的基本解決方案:


/*

 * first, store all records with matching member_id into store $c

 */

$c = [];

foreach ($b as $key0 => $value0) {

    foreach ($a as $key1 => $value1) {

        // matching 'member_id'?

        if ($value0['member_id'] === $value1['member_id']) {

            // inject record into store $c

            $c[] = $a[$key1];

        }

    }

}


/*

 * second, inject non-matching records from $b into store $c

 */

$ids = array_column($c, 'member_id');

foreach ($b as $key => $value) {

    if (!in_array($value['member_id'], $ids)) {

        $sorting = max(array_column($c, 'sorting')) + 1;

        $value['sorting'] = $sorting;

        $c[] = $value;

    }

}

工作演示


查看完整回答
反對 回復(fù) 2023-07-08
  • 2 回答
  • 0 關(guān)注
  • 206 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號