0

I have two arrays which look like this :

$has_end_date = ['value123' , 'value132'];
$has_no_end_date = ['value123' , 'value256'];

Now I wish to return the unique values from $has_no_end_date that doesn't appear in $has_end_date.

This is what I tried :

I tried to merge the two arrays together and then find the unique values. But that did not work, i will get the unique of both of them. I am only looking for the unique different values from $has_no_end_date array.

4
  • 5
    have you tried array_diff($has_no_end_date, $has_end_date) ? If required unique then array_unique Commented Jun 24, 2019 at 7:53
  • 1
    So the answer for this is ['value256']? Commented Jun 24, 2019 at 7:57
  • 1
    @vivek_23 Yes sir Commented Jun 24, 2019 at 7:57
  • 1
    @DrakulaPredator I will take a look at it. Commented Jun 24, 2019 at 7:57

3 Answers 3

3

You need to use array_diff() to get the difference and array_unique() to return unique values.

$difference = array_unique(array_diff($has_no_end_date, $has_end_date));
print_r($difference);

Output:-https://3v4l.org/3oLHA AND https://3v4l.org/7aEFW

Sign up to request clarification or add additional context in comments.

Comments

2

You can use array_diff

$a1 = ['value123' , 'value132'];
$a2 = ['value123' , 'value256'];

$diff = array_diff($a2, $a1);
echo '<pre>';
print_r($diff);

Output :-

Array
(
  [1] => value256
)

Comments

2

You can use array_diff and array_unique if you want to fetch that data,

print_r(array_unique(array_diff($has_no_end_date, $has_end_date)));

Demo.

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.