Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-24-2012, 04:22 PM   PM User | #1
durangod
Senior Coder

 
Join Date: Nov 2010
Posts: 1,177
Thanks: 214
Thanked 31 Times in 30 Posts
durangod is on a distinguished road
Comparing Array

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.

PHP Code:

// $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.

PHP Code:
$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.

Last edited by durangod; 01-24-2012 at 04:44 PM..
durangod is offline   Reply With Quote
Old 01-24-2012, 04:35 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
PHP Code:

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.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
durangod (01-24-2012)
Old 01-24-2012, 04:43 PM   PM User | #3
durangod
Senior Coder

 
Join Date: Nov 2010
Posts: 1,177
Thanks: 214
Thanked 31 Times in 30 Posts
durangod is on a distinguished road
Oh ok got ya, thanks so much.
durangod is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:43 AM.


Advertisement
Log in to turn off these ads.