0

I Have 2 Collections, The first collection comes from a query with a where condition, while the second collection comes from all items. I intend to merge the two collections, but between the first array and the second array there are the same items. How do i make the items unique when they are merged?

Here is first array,

[
    {
        "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"
                }
            }
        ]
    }
]

And here is the second array

[
    {
        "id": 1,
        "id_owner": 3,
        "name": "Janji Jiwa",
        "slug": "JJ",
        "logo": "images/brand/4041569999956.png",
        "created_at": null,
        "updated_at": "2020-03-05 16:02:11",
        "category": "ITEM LAINNYA",
        "brand_items": [
            {
                "id": 1,
                "id_brand": 1,
                "id_brand_category": 1,
                "name": "ALPUKAT",
                "accurate_name": "ALPUKAT",
                "po_name": null,
                "id_unit_order": 2,
                "id_unit_opname": 2,
                "id_unit2_order": null,
                "id_unit2_opname": null,
                "low_stock_limit": 0,
                "price": 25000,
                "minimum_order": 1,
                "image": null,
                "is_inventory": 1,
                "is_ongkos_kirim": 0,
                "limit": "2",
                "created_at": "2020-02-25 09:32:46",
                "updated_at": "2020-02-25 09:32:46",
                "unit_opname": {
                    "id": 2,
                    "name": "Pcs",
                    "created_at": "2019-09-19 00:00:00",
                    "updated_at": "2019-09-19 00:00:00"
                }
            },
            {
                "id": 3,
                "id_brand": 1,
                "id_brand_category": 8,
                "name": "CHOCOLATE",
                "accurate_name": "CHOCOLATE",
                "po_name": null,
                "id_unit_order": 2,
                "id_unit_opname": 2,
                "id_unit2_order": null,
                "id_unit2_opname": null,
                "low_stock_limit": 0,
                "price": 25000,
                "minimum_order": 1,
                "image": null,
                "is_inventory": 1,
                "is_ongkos_kirim": 0,
                "limit": null,
                "created_at": "2020-02-25 09:32:47",
                "updated_at": "2020-02-25 09:32:47",
                "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_category": 9,
                "name": "GULA AREN",
                "accurate_name": "GULA AREN",
                "po_name": null,
                "id_unit_order": 2,
                "id_unit_opname": 2,
                "id_unit2_order": null,
                "id_unit2_opname": null,
                "low_stock_limit": 0,
                "price": 12000,
                "minimum_order": 1,
                "image": null,
                "is_inventory": 1,
                "is_ongkos_kirim": 0,
                "limit": null,
                "created_at": "2020-02-25 09:32:48",
                "updated_at": "2020-02-25 09:32:48",
                "unit_opname": {
                    "id": 2,
                    "name": "Pcs",
                    "created_at": "2019-09-19 00:00:00",
                    "updated_at": "2019-09-19 00:00:00"
                }
            },
            {
                "id": 9,
                "id_brand": 1,
                "id_brand_category": 12,
                "name": "PLASTIC CUP 14 OZ",
                "accurate_name": "PLASTIC CUP 14 OZ",
                "po_name": null,
                "id_unit_order": 2,
                "id_unit_opname": 2,
                "id_unit2_order": null,
                "id_unit2_opname": null,
                "low_stock_limit": 0,
                "price": 17000,
                "minimum_order": 1,
                "image": null,
                "is_inventory": 1,
                "is_ongkos_kirim": 0,
                "limit": null,
                "created_at": "2020-02-25 09:32:48",
                "updated_at": "2020-02-25 09:32:48",
                "unit_opname": {
                    "id": 2,
                    "name": "Pcs",
                    "created_at": "2019-09-19 00:00:00",
                    "updated_at": "2019-09-19 00:00:00"
                }
            },
            {
                "id": 11,
                "id_brand": 1,
                "id_brand_category": 8,
                "name": "ROTI TAWAR",
                "accurate_name": "ROTI TAWAR",
                "po_name": null,
                "id_unit_order": 2,
                "id_unit_opname": 2,
                "id_unit2_order": null,
                "id_unit2_opname": null,
                "low_stock_limit": 0,
                "price": 25000,
                "minimum_order": 1,
                "image": null,
                "is_inventory": 1,
                "is_ongkos_kirim": 0,
                "limit": null,
                "created_at": "2020-02-25 09:32:48",
                "updated_at": "2020-02-25 09:32:48",
                "unit_opname": {
                    "id": 2,
                    "name": "Pcs",
                    "created_at": "2019-09-19 00:00:00",
                    "updated_at": "2019-09-19 00:00:00"
                }
            },
            {
                "id": 14,
                "id_brand": 1,
                "id_brand_category": 13,
                "name": "TISSUE GULUNG",
                "accurate_name": "TISSUE GULUNG",
                "po_name": null,
                "id_unit_order": 2,
                "id_unit_opname": 2,
                "id_unit2_order": null,
                "id_unit2_opname": null,
                "low_stock_limit": 0,
                "price": 17000,
                "minimum_order": 1,
                "image": null,
                "is_inventory": 1,
                "is_ongkos_kirim": 0,
                "limit": null,
                "created_at": "2020-02-25 09:32:48",
                "updated_at": "2020-02-25 09:32:48",
                "unit_opname": {
                    "id": 2,
                    "name": "Pcs",
                    "created_at": "2019-09-19 00:00:00",
                    "updated_at": "2019-09-19 00:00:00"
                }
            }
        ]
    }
]

And here is my code :

public function deliveryIn(Request $request){
        $post = $request->all();


        $data1 = BrandOutletDelivery::where('no_surat', 'LIKE' ,'%'.$post['no_surat'])->where('type', 'Delivery Out')->whereDate('created_at', date("Y-m-d"))->with('brandOutletDeliveryItems.unit')->get();

        if(count($data1) > 0){
            $data1[0]['category']="ITEM MASUK"; //add key & value to the array
            foreach($data1 as $key => $value){
                foreach ($value['brandOutletDeliveryItems'] as $key1 => $val) {


                    $value['brandOutletDeliveryItems'][$key1]['name'] = $val['brandItem']['name'];
                    $value['brandOutletDeliveryItems'][$key1]['id'] = $val['brandItem']['id'];
                    $value['brandOutletDeliveryItems'][$key1]['id_unit_opname'] = $val['brandItem']['id_unit_opname'];

                    $val['unit_opname'] = $val['unit'];

                    unset($val['unit']);
                    unset($val['brandItem']);
                    // $data2 = $data2->get();
                }

                $value['brand_items'] = $value['brandOutletDeliveryItems'];
                   unset($data1[$key]['brandOutletDeliveryItems']);

            }

        }

        $data2 = Brand::where('id', $post['id_brand']);
        $data2->with(['brandItems' => function($q){
            $q->where('is_inventory', 1)->with('unitOpname');
        }]);
        $data2 = $data2->get();

        // }

        $data2[0]['category']="ITEM LAINNYA"; //add key & value to the array


        $data = $data1->merge($data2);


        return response(MyHelper::checkGet($data));


        return ['status' => 'fail', 'messages' => ['Nomor Surat Tidak Ditemukan']];

    }

In the first array there are brand_items with the names "ALPUKAT" and "GULA AREN", and the same items are also in the second array. How can I make the items in the first array not show up again in the second array? I have tried combining two arrays, but still the items are still not unique.

1
  • Write a function using in_array() first it iterates through the first array identifying each value, then iterates through the second array and compares values removing doubled items. Lastly create a new array with the removed items. Commented Mar 10, 2020 at 2:30

3 Answers 3

1

Going by the code you provided, I am going to assume that $data1 and $data2 is a laravel collection class instead of an array, then you can try the following code before your query of 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();
Sign up to request clarification or add additional context in comments.

Comments

1

Use in_array() to find like values in the two arrays. Then find out what the key value of the second arrays duplicates are using array_search(). Unset the duplicates using a foreach loop.

I use two defined arrays of colors in my example

Note the two like variables purple and 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]);            
    }
}

Now merge the two arrays together into one new array using array_merge(): By leaving the first array alone the original value that was duplicated in the second value is defined in the array, just the second value has been removed

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

var_dump($all_colors);

Original variables:

$one = array(
    "green" , "red" , "brown", "purple", "blue", "limegreen"
);
$two = array(
    "yellow" , "orange" , "purple", "blue", "skyblue"
);

Note the key values are indeed different

By defining a new variable $key and assigning the variables that were in_array() within the foreach() loop we can now unset those different keyed variables from our second array, $two.

Results:

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

No duplicates now. Do what you wish with this new cleansed variable.

Bonus:

Wrap this all up in a function.

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

use function evalArrays($somearray, $anotherarray);

2 Comments

in "if(in_array($value, $second_array))" error "in_array() expects parameter 2 to be array, object given in file". i try to $data->toArray(); but when I try to do it again, it doesn't enter "if(in_array($value, $second_array))".
Tammam, I apologize as I did not note that you tagged larvel in your question. My method of combining two arrays and removing duplicates does not tackle your issue. Unfortunately, I have no experience with Larvel. However, have you tried typecasting the object to array? $array = (array) $yourObject; This link may help: ocramius.github.io/blog/fast-php-object-to-array-conversion
-1

You can hash each row by

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

Then store the hashed row into a new column.

2 Comments

Is this possible so that when it appears there are no double items?
You can compare the hashes if they are the same they are a duplicate

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.