0

I want to remove duplicated value between 2 arrays. How can I do?

enter image description here

  • 1, 3, 4, 6 are duplicated in a both arrays, I want to unique values.

I using map() to show arrays of item_id contains of quantity but it's duplicate value, I don't want it.

$deliveries =  $pickupsGroupByDepartment->first()->map(function ($q) {
    return $q->deliveries->groupBy('delivery_date')->map(function($r) {
        return $r->mapToGroups(function ($item) {
              return [$item['item_id'] => $item['quantity']];
         });
     });
});
3
  • In the attached image which are the duplicated entries/values? Commented Aug 28, 2019 at 5:27
  • @KennyHorna 1, 3, 4, 6 are duplicated values of both arrays. Commented Aug 28, 2019 at 5:32
  • I think you could take advantage of the Collections class to filter this data.. the thing is.. what is your logic to group this items.. add what you need/want in the description (it'd be better if you provide an example) so we can help you solve the issue Commented Aug 28, 2019 at 5:59

1 Answer 1

1

You can use array_unique function then combine it using array_merge function.

$array = array_unique (array_merge ($array1, $array2));

if your data is came from object in Laravel. you can use code below.

$result = $object1->merge($object2)->unique();

If the situation is dynamic data you can do this..

$results = [];

foreach($dynamicArray  as $key => $array){

    $results = array_unique (array_merge ($results, $array));

}
return $results;
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, but it's dynamic data. Sometimes it hasn't just 2 arrays or objects. How can I do this?
Just store it in one variable then merge another array into the variable you created,

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.