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 6 of 6
  1. #1
    New Coder
    Join Date
    Feb 2009
    Posts
    25
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Exclamation getting unique occurences (no duplicates) from an array..

    I have an array, containing many custom objects which represent auction bids.
    A bid obect has these variables:
    $bidAmount
    $username

    What I have:
    so I basically have an array, with many bids in it:
    array[new Bid(), new Bid(), new Bid(), new Bid()];
    So if i wanted to access the username of the first bid, I would do:
    $array[0]->username

    What I want to get out of it:
    How can I get the usernames out of that, where the userIDs are unique. So if these are the bids
    PHP Code:
    echo $array[0]->username//prints sam
    echo $array[1]->username//prints john
    echo $array[2]->username//prints peter
    echo $array[3]->username//prints peter
    echo $array[4]->username//prints sam
    echo $array[5]->username//prints sam 
    So, How do I only get these 3 reults without duplicating the usernames:
    sam, john, peter

  • #2
    Regular Coder
    Join Date
    Apr 2005
    Location
    Ohio
    Posts
    254
    Thanks
    1
    Thanked 63 Times in 63 Posts

  • #3
    New Coder
    Join Date
    Feb 2009
    Posts
    25
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Thanks but how do I get array_unique() to work with a variable ($username) within an object, and not the other variables there?

  • #4
    New Coder
    Join Date
    Feb 2009
    Posts
    38
    Thanks
    0
    Thanked 1 Time in 1 Post
    Quote Originally Posted by zachbb View Post
    I have an array, containing many custom objects which represent auction bids.
    A bid obect has these variables:
    $bidAmount
    $username

    What I have:
    so I basically have an array, with many bids in it:
    array[new Bid(), new Bid(), new Bid(), new Bid()];
    So if i wanted to access the username of the first bid, I would do:
    $array[0]->username

    What I want to get out of it:
    How can I get the usernames out of that, where the userIDs are unique. So if these are the bids
    PHP Code:
    echo $array[0]->username//prints sam
    echo $array[1]->username//prints john
    echo $array[2]->username//prints peter
    echo $array[3]->username//prints peter
    echo $array[4]->username//prints sam
    echo $array[5]->username//prints sam 
    So, How do I only get these 3 reults without duplicating the usernames:
    sam, john, peter
    You can make a new array and the values of this array in that array
    PHP Code:
    $arr = array();
    $arr[0] = $array[0]->username;
    $arr[1] = $array[1]->username;
    $arr[2] = $array[2]->username;
    $arr[3] = $array[3]->username;
    $arr[4] = $array[4]->username;
    $arr[5] = $array[5]->username;
    print_r(array_unique($arr)); 
    The above code will give you all the distinct and unique values from the array.

  • #5
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by milanchheda View Post
    You can make a new array and the values of this array in that array
    PHP Code:
    $arr = array();
    $arr[0] = $array[0]->username;
    $arr[1] = $array[1]->username;
    $arr[2] = $array[2]->username;
    $arr[3] = $array[3]->username;
    $arr[4] = $array[4]->username;
    $arr[5] = $array[5]->username;
    print_r(array_unique($arr)); 
    The above code will give you all the distinct and unique values from the array.
    no, this will get an error, because don't work with object.

    http://www.php.net/manual/en/function.array-unique.php

    best regards

  • #6
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    try this way:
    PHP Code:
    <?php
    class {
      var 
    $username;

      function 
    __construct($u){
        
    $this->username $u;
      }
    }

    $a = array(new a('sam'),
           new 
    a('bill'),
           new 
    a('sam'),
           new 
    a('sam'));
    foreach(
    $a as $o){
      print 
    $o->username "\n";
    }

    function 
    obj_array_unique($a){
      
    $objs = array();
      
    $keys = array();
      while(
    count($a) > 0){
        
    $obj array_shift($a);
        if(!
    in_array($obj->username,$keys)){
          
    $objs[] = $obj;
          
    $keys[] = $obj->username;
        }
      }
      return 
    $objs;
    }


    $a obj_array_unique($a);
    print 
    "-------\n";
    foreach(
    $a as $o){
      print 
    $o->username "\n";
    }
    ?>
    best regards

  • Users who have thanked oesxyl for this post:

    zachbb (03-03-2009)


  •  

    Posting Permissions

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