...

View Full Version : getting unique occurences (no duplicates) from an array..



zachbb
03-01-2009, 05:46 PM
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

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

shadowmaniac
03-01-2009, 08:03 PM
array_unique() (http://www.php.net/function.array-unique)

zachbb
03-01-2009, 08:51 PM
Thanks but how do I get array_unique() to work with a variable ($username) within an object, and not the other variables there?

milanchheda
03-02-2009, 04:50 AM
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

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

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

oesxyl
03-03-2009, 03:52 AM
You can make a new array and the values of this array in that array

$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

oesxyl
03-03-2009, 04:20 AM
try this way:


<?php
class a {
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum