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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何合并集合關(guān)系數(shù)組并使其唯一?

如何合并集合關(guān)系數(shù)組并使其唯一?

PHP
紅糖糍粑 2022-10-22 16:49:25
我有 2 個(gè)集合,第一個(gè)集合來(lái)自帶有 where 條件的查詢,而第二個(gè)集合來(lái)自所有項(xiàng)目。我打算合并這兩個(gè)集合,但是在第一個(gè)數(shù)組和第二個(gè)數(shù)組之間有相同的項(xiàng)目。合并后如何使項(xiàng)目獨(dú)一無(wú)二?這是第一個(gè)數(shù)組,[    {        "id": 71,        "id_brand": 1,        "id_brand_outlet": 14,        "id_user": 5,        "id_brand_outlet_tujuan": 15,        "alasan": "Try",        "no_surat": "JJ-00001-1",        "created_at": "2020-03-10 08:57:00",        "updated_at": "2020-03-10 08:57:00",        "type": "Delivery Out",        "category": "ITEM MASUK",        "brand_items": [            {                "id": 1,                "id_brand": 1,                "id_brand_delivery": 71,                "id_brand_item": 1,                "id_unit": 2,                "qty_before": 10,                "qty_change": 10,                "qty_after": 0,                "created_at": "2020-03-10 08:57:00",                "updated_at": "2020-03-10 08:57:00",                "name": "ALPUKAT",                "id_unit_opname": 2,                "unit_opname": {                    "id": 2,                    "name": "Pcs",                    "created_at": "2019-09-19 00:00:00",                    "updated_at": "2019-09-19 00:00:00"                }            },            {                "id": 8,                "id_brand": 1,                "id_brand_delivery": 71,                "id_brand_item": 8,                "id_unit": 2,                "qty_before": 10,                "qty_change": 10,                "qty_after": 0,                "created_at": "2020-03-10 08:57:00",                "updated_at": "2020-03-10 08:57:00",                "name": "GULA AREN",                "id_unit_opname": 2,                "unit_opname": {                    "id": 2,                    "name": "Pcs",                    "created_at": "2019-09-19 00:00:00",                    "updated_at": "2019-09-19 00:00:00"                }            }        ]    }]在第一個(gè)數(shù)組中有brand_items名稱“ALPUKAT”和“GULA AREN”,第二個(gè)數(shù)組中也有相同的項(xiàng)目。如何使第一個(gè)數(shù)組中的項(xiàng)目不再出現(xiàn)在第二個(gè)數(shù)組中?我嘗試過組合兩個(gè)數(shù)組,但這些項(xiàng)目仍然不是唯一的。
查看完整描述

3 回答

?
江戶川亂折騰

TA貢獻(xiàn)1851條經(jīng)驗(yàn) 獲得超5個(gè)贊

按照您提供的代碼,我將假設(shè)$data1and$data2是一個(gè) laravel 集合類而不是一個(gè)數(shù)組,那么您可以在查詢 data2 之前嘗試以下代碼。


// get the id of all the brand item of the first array

$brandIdArr = $data1->pluck('brand_items')->flatten()->pluck('id')->all();


// then filter them out on your eager loading query

$data2 = Brand::where('id', $post['id_brand']);

$data2->with(['brandItems' => function($q)use($brandIdArr){ // added use

    $q->where('is_inventory', 1)->whereNotIn('id',$brandIdArr)->with('unitOpname'); // added whereNotIn

}]);

$data2 = $data2->get();


查看完整回答
反對(duì) 回復(fù) 2022-10-22
?
眼眸繁星

TA貢獻(xiàn)1873條經(jīng)驗(yàn) 獲得超9個(gè)贊

用于in_array()在兩個(gè)數(shù)組中查找相似的值。然后找出第二個(gè)數(shù)組副本的鍵值正在使用什么array_search()。使用 foreach 循環(huán)取消設(shè)置重復(fù)項(xiàng)。


我在示例中使用了兩個(gè)已定義的顏色數(shù)組


注意兩個(gè)相似的變量purple和blue


$one = array(

    "green" , "red" , "brown", "purple", "blue", "limegreen"

);

$two = array(

    "yellow" , "orange" , "purple", "blue", "skyblue"

);




foreach($one as $key => $color){

    if(in_array($color, $two)){            

        $key = array_search($color, $two);

            unset($two[$key]);            

    }

}

現(xiàn)在使用以下方法將兩個(gè)數(shù)組合并為一個(gè)新數(shù)組array_merge(): 通過單獨(dú)保留第一個(gè)數(shù)組,在數(shù)組中定義在第二個(gè)值中重復(fù)的原始值,僅刪除第二個(gè)值


$all_colors = array_merge($one, $two);


var_dump($all_colors);

原始變量:


$one = array(

    "green" , "red" , "brown", "purple", "blue", "limegreen"

);

$two = array(

    "yellow" , "orange" , "purple", "blue", "skyblue"

);

注意鍵值確實(shí)不同


通過定義一個(gè)新變量$key并分配循環(huán)in_array()內(nèi)的變量,foreach()我們現(xiàn)在可以從我們的第二個(gè)數(shù)組中取消設(shè)置那些不同的鍵控變量,$two.


結(jié)果:


array(9) { 

    [0]=> string(5) "green" 

    [1]=> string(3) "red" 

    [2]=> string(5) "brown" 

    [3]=> string(6) "purple" 

    [4]=> string(4) "blue" 

    [5]=> string(9) "limegreen" 

    [6]=> string(6) "yellow" 

    [7]=> string(6) "orange" 

    [8]=> string(7) "skyblue" 

}

現(xiàn)在沒有重復(fù)。用這個(gè)新的清理變量做你想做的事。


獎(jiǎng)金:


將這一切包裝在一個(gè)函數(shù)中。


function evalArrays($first_array, $second_array){

    foreach($first_array as $k => $value){

        if(in_array($value, $second_array)){            

            $keyvalues_to_remove = array_search($value, $second_array);

                unset($second_array[$keyvalues_to_remove]);            

        }

    }

    $new_array = array_merge($first_array, $second_array);

    return $new_array;

}

使用功能evalArrays($somearray, $anotherarray);


查看完整回答
反對(duì) 回復(fù) 2022-10-22
?
絕地?zé)o雙

TA貢獻(xiàn)1946條經(jīng)驗(yàn) 獲得超4個(gè)贊

您可以通過散列每一行

$hashedArrayRow = md5(serialize($arrayRow));

然后將散列行存儲(chǔ)到新列中。


查看完整回答
反對(duì) 回復(fù) 2022-10-22
  • 3 回答
  • 0 關(guān)注
  • 126 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

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