2013-04-30 11 views
1

我有以下PHP stdclass對象數組(實際上,我的數據庫中有數百個索引,但目標相同),而且我無法找到最有效的方法來確定通過PHP編程,stdclass對象最常出現在數組中。確定哪個PHP stdclass對象最經常出現在數組中

例如,如果索引0處的標準類對象最常出現(顯然它不),那麼我想回顯「1,1024」或類似的東西。

 
Array 
(
    [0] => stdClass Object 
     (
      [a] => 1 
      [b] => 1024 
     ) 

    [1] => stdClass Object 
     (
      [a] => 4 
      [b] => 4096 
     ) 

    [2] => stdClass Object 
     (
      [a] => 4 
      [b] => 4096 
     ) 

    [3] => stdClass Object 
     (
      [a] => 4 
      [b] => 4096 
     ) 

    [4] => stdClass Object 
     (
      [a] => 4 
      [b] => 4096 
     ) 

    [5] => stdClass Object 
     (
      [a] => 4 
      [b] => 4096 
     ) 

    [6] => stdClass Object 
     (
      [a] => 4 
      [b] => 4096 
     ) 

    [7] => stdClass Object 
     (
      [a] => 4 
      [b] => 6144 
     ) 

    [8] => stdClass Object 
     (
      [a] => 4 
      [b] => 6144 
     ) 

    [9] => stdClass Object 
     (
      [a] => 8 
      [b] => 6144 
     ) 

    [10] => stdClass Object 
     (
      [a] => 8 
      [b] => 6144 
     ) 

    [11] => stdClass Object 
     (
      [a] => 8 
      [b] => 8192 
     ) 

    [12] => stdClass Object 
     (
      [a] => 8 
      [b] => 8192 
     ) 

    [13] => stdClass Object 
     (
      [a] => 8 
      [b] => 8192 
     ) 

    [14] => stdClass Object 
     (
      [a] => 8 
      [b] => 8192 
     ) 
) 

謝謝!

+0

你的方法是什麼? – 2013-04-30 21:54:45

+0

你是什麼意思「哪個stdclass對象最經常出現在數組中」?每個對象都是一個實例。你的意思是「具有相同a,b對的物體」嗎? – 2013-04-30 21:58:56

+0

正如第一條評論所述,到目前爲止你的方法是什麼? – dbf 2013-04-30 22:03:12

回答

0

雖然我沒有與任何答案一起,具體來說,他們都非常有幫助我想出了這個解決方案。

$x = array(); 
$count = 0; 

for ($i = 0; $i < count($myArray); $i++) { 
    if ($myArray[$i]->a != $myArray[$i - 1]->a 
     && $myArray[$i]->b != $myArray[$i - 1]->b) { 
     $count = 0; 
    } 

    $x[$myArray[$i]->a . '|' . $myArray[$i]->b] = ++$count; 
} 

print_r($x); 

這將導致以下輸出。

 
Array 
(
    [1|1024] => 1 
    [4|4096] => 5 
    [4|6144] => 3 
    [8|6144] => 2 
    [8|8192] => 4 
) 

然後只是從該數組中獲取最大值。

0

搜索所有寄存器,將[a]出現存儲在數組/映射中並按計數排序?

0

我認爲你需要這樣的東西吧?我認爲這很快。如果您需要檢查相同對象或類似的實例,可以使用spl_object_hash函數生成密鑰,而不是像我那樣連接值。

$foo = (object)['a' => 0, 'b' => 1024]; 
$bar = (object)['a' => 4, 'b' => 4096]; 
$qux = (object)['a' => 8, 'b' => 6144]; 

$array = []; 
foreach(range(1, 1024) as $i) $array[] = $foo; 
foreach(range(1, 4096) as $i) $array[] = $bar; 
foreach(range(1, 6144) as $i) $array[] = $qux; 

$result = []; 
array_walk($array, function($obj) use (&$result) { 
    $key = implode('|', get_object_vars($obj)); 
    if (isset($result[$key])) $result[$key]++; 
    else $result[$key] = 1; 
}); 

arsort($result); 
print_r($result); 

返回:

Array 
(
    [8|6144] => 6144 
    [4|4096] => 4096 
    [0|1024] => 1024 
) 

問候。

0

事實證明,==運營商會告訴你if objects are equal。因此,您可以循環訪問數組中的每個項目,並構建另一個數組來跟蹤每個值的遇到頻率。然後你需要通過計數對結果數組排序,這樣你就知道最經常發生哪個對象。

$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]);