note 40046 added to function.array-unique
| From: | bitmore dot co dot kr at rn2 dot php dot net | Date: | Thu, 19 Feb 2004 20:53:49 +0000 |
| Subject: | note 40046 added to function.array-unique | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-65425@lists.php.net to get a copy of this message | ||
//Modify
Object Unique
class foo {
var $_name;
var $_age;
function foo($name,$age=NULL) { $this->_name = $name; $this->_age = $age; }
//function get() { return $this->_name; }
//function set($name,$age=NULL) { $this->_name = $name; $this->_age = $age; }
}
function DistinctOn ($obj, $item) {
$out = array();
$list = array();
foreach ($obj as $key=>$so) {
if (!in_array($so->$item, $list)) {
echo "key = $key,so = $so,item = $item,IFlist = ";print_r($list);echo
"<br>";
$list[] = $so->$item;//°ËÁõ¹è¿
$out[$key] = $so;
}
echo "Forlist = ";print_r($list);echo "<br>";
}
return $out;
}
$foo_obj[0] = new foo('tom',20);
$foo_obj[1] = new foo('paul',66);
$foo_obj[2] = new foo('tom',23);
$item = '_name';
$result = DistinctOn ($foo_obj, $item);
while(list($k,$v) = each($result)) {
print "K = ";print_r($k);
print ",V = ";print_r($v);
print "<br>";
}
//key = 0,so = Object,item = _name,IFlist = Array ( ) ,Forlist = Array ( [0] => tom )
//key = 1,so = Object,item = _name,IFlist = Array ( [0] => tom ) ,Forlist = Array ( [0] =>
tom [1] => paul )
//Forlist = Array ( [0] => tom [1] => paul )
//K = 0,V = foo Object ( [_name] => tom [_age] => 20 )
//K = 1,V = foo Object ( [_name] => paul [_age] => 66 )
print "<br><br>";
$item = '_age';
$result = DistinctOn ($foo_obj, $item);
while(list($k,$v) = each($result)) {
print "K = ";print_r($k);
print ",V = ";print_r($v);
print "<br>";
}
//key = 0,so = Object,item = _age,IFlist = Array ( ) ,Forlist = Array ( [0] => 20 )
//key = 1,so = Object,item = _age,IFlist = Array ( [0] => 20 ) ,Forlist = Array ( [0] => 20
[1] => 66 )
//key = 2,so = Object,item = _age,IFlist = Array ( [0] => 20 [1] => 66 ) ,
// Forlist = Array ( [0] => 20 [1] => 66 [2] => 23 )
//K = 0,V = foo Object ( [_name] => tom [_age] => 20 )
//K = 1,V = foo Object ( [_name] => paul [_age] => 66 )
//K = 2,V = foo Object ( [_name] => tom [_age] => 23 )
----
Manual Page -- http://www.php.net/manual/en/function.array-unique.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+40046
Delete -- http://master.php.net/manage/user-notes.php?action=delete+40046&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+40046&report=yes
Search -- http://master.php.net/manage/user-notes.php