如何合并集合关系数组并使其唯一?

我有 2 个集合,第一个集合来自带有 where 条件的查询,而第二个集合来自所有项目。我打算合并这两个集合,但是在第一个数组和第二个数组之间有相同的项目。合并后如何使项目独一无二?


这是第一个数组,


[

    {

        "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"

                }

            }

        ]

    }

]

在第一个数组中有brand_items名称“ALPUKAT”和“GULA AREN”,第二个数组中也有相同的项目。如何使第一个数组中的项目不再出现在第二个数组中?我尝试过组合两个数组,但这些项目仍然不是唯一的。



红糖糍粑
浏览 156回答 3
3回答

江户川乱折腾

按照您提供的代码,我将假设$data1and$data2是一个 laravel 集合类而不是一个数组,那么您可以在查询 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();

眼眸繁星

用于in_array()在两个数组中查找相似的值。然后找出第二个数组副本的键值正在使用什么array_search()。使用 foreach 循环取消设置重复项。我在示例中使用了两个已定义的颜色数组注意两个相似的变量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]);                }}现在使用以下方法将两个数组合并为一个新数组array_merge(): 通过单独保留第一个数组,在数组中定义在第二个值中重复的原始值,仅删除第二个值$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");注意键值确实不同通过定义一个新变量$key并分配循环in_array()内的变量,foreach()我们现在可以从我们的第二个数组中取消设置那些不同的键控变量,$two.结果: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" }现在没有重复。用这个新的清理变量做你想做的事。奖金:将这一切包装在一个函数中。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);

绝地无双

您可以通过散列每一行$hashedArrayRow = md5(serialize($arrayRow));然后将散列行存储到新列中。
打开App,查看更多内容
随时随地看视频慕课网APP