0

Say I have these two array:

$arraryA = array(10587,10590,10598,10592,10602,10604,10607);

$arrayB = array(10590);

What I know, is that,

  1. values of $arrayB will always be elements from $arrayA
  2. $arrayB may have one or more elements

I need to remove the values of $arrayB from $arrayA.

Means, I need a new array as:

$arrayC = array(10587,10598,10592,10602,10604,10607);

if $arrayB = array(10590, 10604), $arrayC will be:

$arrayC = array(10587,10598,10592,10602,10607);

Any idea ?

5
  • 2
    You've not though to look at array_diff() then? Commented Mar 4, 2013 at 19:46
  • That's it Mark. How can I accept your reply ? Can you post a new reply please so that I can accept it ? Commented Mar 4, 2013 at 19:47
  • Question, do I need to check if $arrayB is present as an array with array_diff, or that checks it automatically ? Commented Mar 4, 2013 at 19:48
  • 1
    I can't recall if the second argument passed to array_diff() must be an array, but suspect that this is probably the case Commented Mar 4, 2013 at 19:52
  • Right, I checked it. $arrayB must be present as an array, else it throws error. Commented Mar 4, 2013 at 19:53

1 Answer 1

3

Use array_diff()

$arrayC= array_diff($arrayA, $arrayB);

Example:

$arrayA = array(10587,10590,10598,10592,10602,10604,10607);
$arrayB  = array(10590);
$arrayC  = array_diff($arrayA, $arrayB);

var_dump($arrayC);

// array(6) { [0]=> int(10587) [2]=> int(10598) [3]=> int(10592) [4]=> int(10602) [5]=> int(10604) [6]=> int(10607) }

See it in action

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.