0

If you do an array_diff between two values:

$ignore = array_diff($arr1, $arr2);

How can you make it avoid removing one specific value from $arr1?

0

1 Answer 1

1

Here are two arrays for example.

$arr = ['something', 'another thing', 'three things'];
$arr = ['something', 'another thing', 'different thing'];

And the value you want to keep:

$keep = 'something';

If you need that certain value to always be there, it probably would be easiest to just explicitly add it after doing the diff.

$ignore = array_diff($arr1, $arr2);
$ignore[] = $keep;

but if you only need it to not be removed if it is present, then you can use array_udiff.

$ignore = array_udiff($arr1, $arr2, function($a, $b) use ($keep) {
    if ($a == $keep) return -1;
    return $a == $b ? 0 : 1;
});
Sign up to request clarification or add additional context in comments.

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.