5

The $category variable will either hold values (i.e. tags) that are already available in the tags table, or the user can enter new tags in this variable , or both.

Following is the query that brings tags if they are available in the tags table:

$TagNames = DB::table('tags')
                    ->whereIn('t_name', $category)                  
                    ->pluck('t_name'); 

The output of TagNames :

array(2) { [0]=> string(7) "Zamalek" [1]=> string(4) "Ahly" }

Below is the $category variable that will hold all the tags, regardless they are new tags entered by the user, or existing ones.

$category = $request->get('catBox');

Output of $category (with two additional new tags) :

array(4) { [0]=> string(7) "Zamalek" [1]=> string(4) "Ahly" [2]=> string(5) "Gouna" [3]=> string(8) "Pyramids" }

The question is : How can I compare both arrays and get only the difference (i.e. "Gouna" and "pyramids" in this case) in a new array ?

4 Answers 4

10

If you need to retrieve only the differences from the $category.

You can use PHP inbuild function array_diff() function.

$differenceArray = array_diff($category, $TagNames);

Keep in mind that, it will not display the difference of $TagNames.

To retrieve all the difference, you can retrieve the first difference and second difference and use array_merge() function to merge it together.

$differenceArray1 = array_diff($category, $TagNames);
$differenceArray2 = array_diff($TagNames, $category);

$mergeDifference = array_merge($differenceArray1, $differenceArray2);
Sign up to request clarification or add additional context in comments.

Comments

4

You can use array_diff(A,B), it returns all elements from A, which are not elements of B (= A without B).

<?php
$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);

print_r($result);
?>

Output of difference array1 from array2 is in this output:

Array
(
    [1] => blue
)

Hope it helps.

Comments

2

You can use php function named array_diff and it will return you non matching items by comparing values.

<?php
    $array1 = array(0=> 'Language', 1=>'Math', 2=>'Science', 3=>'Geography');
    $array2 = array(0=> 'Math', 1=>'Science', 2=>'History');
    $diff_result = array_diff($array1, $array2);
    print_r($diff_result);
?>

Output:

Array ( [0] => Language [3] => Geography) 

Ref: https://www.w3resource.com/php/function-reference/array_diff.php

Comments

0

Here's the Laravel way, for those that want to stick with Laravel helpers instead of array_diff for whatever reason.

Let's say you have collection 1 called $activeUserIds and collection 2 called $allUserIds and you want to find the inactive users IDs...

$inactiveUserIds = $activeUserIds->diff($allUserIds);

Comments

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.