Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: Comparing Array

  1. #1
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,415
    Thanks
    269
    Thanked 32 Times in 31 Posts

    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.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.

  • Users who have thanked Fou-Lu for this post:

    durangod (01-24-2012)

  • #3
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,415
    Thanks
    269
    Thanked 32 Times in 31 Posts
    Oh ok got ya, thanks so much.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •