As it turns out, the ==
operator will tell you if objects are equal. So you can just loop through each item in the array, and build another array that keeps track of how often each value is encountered. Then you'll need to sort the resulting array by count, so you know which object occurred most often.
$orig = array(
(object)array('a'=>3, 'b'=>'4096'),
(object)array('a'=>2, 'b'=>'2048'),
(object)array('a'=>2, 'b'=>'2048'),
(object)array('a'=>1, 'b'=>'1024'),
(object)array('a'=>1, 'b'=>'1024'),
(object)array('a'=>2, 'b'=>'2048'),
(object)array('a'=>1, 'b'=>'1024'),
(object)array('a'=>2, 'b'=>'2048'),
);
$countArray = array();
foreach($orig as $obj) {
$didCount = false;
foreach($countArray as $counted) {
if ($counted->value == $obj) {
$counted->count++;
// if we found a match, record that fact and
// break out of this loop early.
$didCount = true;
break;
}
}
// If no match was found, then this is the first time
// we've seen this particular value
if (!$didCount)
$countArray[] = (object)array(
'count' => 1,
'value' => $obj,
);
}
// To find the most frequent item, best way is to
// sort $countArray by count.
usort($countArray, function($left, $right) {
return $right->count - $left->count ;
});
print_r($countArray[0]);