0

当我做

foreach($hugeobject as $k => $obj){
  echo $obj->userid . " = " . $obj->itemid."<br/>";
}

结果如下:

147099 = 594088
147099 = 594086
147099 = 594085
147099 = 594082
147099 = 594083
147099 = 594089
161676 = 594091
196351 = 593971
196351 = 593904
196387 = 593894
196382 = 593892
196382 = 593891
196382 = 593889
196382 = 593888
196351 = 593887
196351 = 593865
196351 = 593864
196722 = 593798
196351 = 593795
196351 = 593794
196351 = 593793
196351 = 593792
196722 = 593790
196351 = 593789

目标是仅显示这些:

147099 = 594088
161676 = 594091
196351 = 593971
196382 = 593892
196722 = 593798

我尝试使用array_unique()甚至将其与array_map()and结合使用serialize()……但都没有帮助。那么我该怎么做呢?

4

2 回答 2

2

试着用 common foreach-loop 写这个:

<?php

$buffer = array();

foreach($hugeobject as $obj){
    if(isset($buffer[$obj->userid]))continue;

    $buffer[$obj->userid] = $obj;
}

foreach($buffer as $obj){
    echo $obj->userid . " = " . $obj->itemid . "<br/>";
}

?>
于 2013-06-06T06:42:17.723 回答
-1

如果您的数据在数组中,请尝试以下操作:

$result = array();
foreach($hugeobject as $k => $obj){
    if(!isset($result[$obj->userid])){
        $result[$obj->userid] = $obj->itemid;
    }
}
于 2013-06-06T06:37:35.720 回答