1

I am have two arrays $A(array of object) and $B

  $A = 
  Array
  (
  [0] => objval Object
      (

      [id_groupe] => 51
      )

  [1] => objval Object
      (

      [id_groupe] => 46
      )

  [2] => objval Object
      (
      [id_groupe] => 52
      )

  )


  $B = 
  Array(51,46)

I want to return new one , if it's id_groupe of $A exisiting in $B so it will be expected result like this:

  Array
  (

  [0] => objval Object
      (
      [id_groupe] => 52
      )

  )

Any could help me?

3
  • Actually that's just set minus operation, ain't it? Commented Feb 21, 2011 at 10:50
  • yes,the new of array object just remove the $A if ele of $A existing in $B Commented Feb 21, 2011 at 11:12
  • please look my update question? Commented Feb 21, 2011 at 11:32

1 Answer 1

1

Ok, this will solve your problem:

// object class
class xy{

    public $id_groupe = 0;

    function xy($id_groupe){
        $this->id_groupe = $id_groupe;
    }


}

// initialize test case Array A
$A = array();
$A[] = new xy(51);
$A[] = new xy(46);
$A[] = new xy(52);

// initialize test case Array B
$B = array(46,51);

// init result array
$diff = array();

// Loop through all elements of the first array
foreach($A as $elem)
{
  // Loop through all elements of the second loop
  // If any matches to the current element are found,
  // they skip that element
  foreach($B as $elem2)
  {
    if($elem->id_groupe == $elem2) continue 2;
  }
  // If no matches were found, append it to $diff
  $diff[] = $elem;
}

// test Array
print_r($diff);
Sign up to request clarification or add additional context in comments.

5 Comments

$A is array of object $B is an array.
$elem->id_groupe,BUT from you code I still get all elements of $A ?
yes,Thariama ,it works,Could you explain me what "continue 2;" mean?
"continue 2" triggers "continue" not on the inner loop but the outer loop. using "continue 3" continue would be called in the outer-outer loop (if this exists)
@Thariama, Thanks,for your explanation.

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.