How can I form two arrays to one array object?
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 cou开发者_StackOverflow社区ld help me?
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);
精彩评论