...

View Full Version : Resolved Comparing Array



durangod
01-24-2012, 05:22 PM
Hi i have a member array and then i have a pic array, what i am trying to do is delete
every member that does not have a pic.

I have already isolated both into their own arrays. Now what i need to do is compare the two arrays.

The rule is if the user id does not exist in the pic array then delete the whole user profile.

This is what i have so far.




// $arrayful is the array of all the members in the system

// $arrayb is the array of all the members with pics

// i want to compare both arrays and those members that do not have pics
// need to be deleted from system.



---arrayful looks like this--

Array
(
[0] => stdClass Object
(
[mem_userid] => 247
)

---arrayb looks like this--

Array
(
[0] => stdClass Object
(
[pic_userid] => 263
[adv_userid] => 263
[adv_sex] => F
[adv_approved] => 1
[adv_paused] => N
)





i want to compare mem_userid to adv_userid and here is what im trying to do in order to get a final array of who i should delete
this resulting array is everyone who needs to be deleted and i will run it thru a loop to delete them.



$result_delwho = array_diff($arrayful, $arrayb);




here is the error i get,
Catchable fatal error: Object of class stdClass could not be converted to string

do i need to use [] ?

Thanks.

Fou-Lu
01-24-2012, 05:35 PM
No, you can't compare them this way. Array comparisons work on the string value, and there is no __tostring overload of a stdclass object.
To use an array_diff you'll need to expand it to use array_udiff and write a custom comparator.



function cmpStdclassObjs($a, $b)
{
return $a->mem_userid == $b->adv_userid;
}

$result_delwho = array_udiff($arrayful, $arrayb, 'cmpStdclassObjs');

You may want to check if a field exists prior to comparing. Since these are stdclass objects you cannot guarantee that $a and $b contain the fields necessary like you can with custom objects.

durangod
01-24-2012, 05:43 PM
Oh ok got ya, thanks so much.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum