确定哪个PHP stdclass对象最常出现在数组中

时间:2013-04-30 21:50:47

标签: php arrays 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
        )
)

谢谢!

4 个答案:

答案 0 :(得分:0)

搜索所有寄存器,将[a]事件存储在数组/映射中并按计数排序?

答案 1 :(得分: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
)

问候。

答案 2 :(得分: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]);

答案 3 :(得分: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
)

然后从该数组中获取最大值。