2

我已经阅读并尝试从那里的嵌套数组线程中应用几乎所有删除重复,我相信这个问题有点独特,因为我试图从(非常)大的多维数组中删除整个重复分支。我想这更像是从数组类型的问题中删除重复数组?

我在Pastebin上有一个转储可以查看。我正在尝试使用一种受保护的方法,我正在调用 superUnique 来消除欺骗,但它不起作用(如下所示)。我究竟做错了什么?

/**
 * @param $array
 * @param bool $preserveKeys
 * @param array $hashes
 * @return array
 */
protected function superUnique($array, $preserveKeys = false, $hashes = array())
{
    $uniqueArray = array();

    foreach ($array AS $key => $value)
    {
        if (TRUE === is_array($value))
        {
            $hash = md5(serialize($value));

            if (FALSE === isset($hashes[$hash]))
            {
                $hashes[$hash] = $hash;
                $uniqueArray[$key] = $this->superUnique($value, $preserveKeys, $hashes);
            } else {
                // skip it i guess ?? should be a duplicate
            }

        } else {

            if ($preserveKeys)
            {
                $uniqueArray[$key] = $value;
            } else {
                $uniqueArray[] = $value;
            }
        }
    }
    return $uniqueArray;
}

这是它运行的代码,以及数组中的重复性示例

    $output = $this->superUnique($output, 1);

    foreach ($output AS $num => $arr)
    {
        // turns a multidim array to an object recursively
        $obj = $this->arrToObj($arr);

        if (isset($obj->message->body))
        {
            echo "Arr#:   {$num}\n";
            echo "Time:   {$obj->attributes->timestamp}\n";
            echo "Body:   {$obj->message->body}\n\n\n";
        }
    }

    die;

这是我输出的一部分,它显示了基于 pastebin 数组的高度重复性。

Arr#:   172
Time:   2013-06-25T16:34:46-0700
Body:   ok, so we decided on everything then?


Arr#:   173
Time:   2013-06-25T16:34:46-0700
Body:   ok, so we decided on everything then?


Arr#:   174
Time:   2013-06-25T16:34:46-0700
Body:   ok, so we decided on everything then?


Arr#:   175
Time:   2013-06-25T16:34:46-0700
Body:   ok, so we decided on everything then?


Arr#:   176
Time:   2013-06-25T16:34:59-0700
Body:   yes, see you tomorrow


Arr#:   177
Time:   2013-06-25T16:34:59-0700
Body:   yes, see you tomorrow


Arr#:   178
Time:   2013-06-25T16:34:59-0700
Body:   yes, see you tomorrow


Arr#:   179
Time:   2013-06-25T16:34:59-0700
Body:   yes, see you tomorrow


Arr#:   180
Time:   2013-06-25T16:35:38-0700
Body:   are you still onlne?


Arr#:   181
Time:   2013-06-25T16:36:10-0700
Body:   hey bob
4

1 回答 1

0

关闭,没有重复,tofrom字段不一样。

我想出的解决方案是删除并重新添加消息属性,这将这些字段从程序逻辑中取出,然后通过将删除的哈希与当前键匹配来将它们重新附加到下一行。干杯,希望这对某人有所帮助。

protected $patterns  = array(
    '/((?=_).*?)@.*/',          // replacing all @'s with leading underscore
    '/_+/i',                    // replacing first occurrence of underscore with @
    '/.*\//i',                  // Group chat modifier to multiple people, same from
);

protected $replace   = array(
    '$1',                       // replace with look back
    '@',                        // replace with (at)
    '',                         // replace with blank
);


..................


/**
 * Remove duplicity
 *
 * @param $array
 * @return array
 *
 * NOTE: always want keys so removed a "preserve" flag
 */
protected function superUnique($array)
{
    $uniqueArray =
    $hashes      = array();

    foreach ($array AS $key => $value)
    {
        // secondary storage of array as object
        $obj = $this->arrToObj($value);

            // remove items causing duplicity issues ....
            unset(
                $value['message']['attributes']['to'],
                $value['message']['attributes']['from']
            );

        if (TRUE === is_array($value))
        {
            // create out serializable hash
            $hash = md5(serialize($value));

            if (FALSE === array_key_exists($hash, $hashes))
            {
                // store as hashmap, remember place in array
                $hashes[$hash] = $key;

                // always preserve keys
                $uniqueArray[$key] = $value;

                // pregging inner content
                if (isset($obj->message->delay->attributes))
                {
                    foreach ($value['message']['delay']['attributes'] AS $name => $pregable)
                    {
                        $uniqueArray[$key]['message']['delay']['attributes'][$name] = $this->preg($pregable);
                    }
                }

                // initial hydration of array
                $uniqueArray[$key]['message']['attributes'][self::members] = array(
                    'to'    => array($this->preg($obj->message->attributes->to)),
                    'from'  => array($this->preg($obj->message->attributes->from)),
                );

            } else {

                // rehydrate array
                $uniqueArray[$hashes[$hash]]['message']['attributes'][self::members] = $this->fuse(
                    $uniqueArray[$hashes[$hash]]['message']['attributes'][self::members],
                    array(
                        'to'    => array($this->preg($obj->message->attributes->to)),
                        'from'  => array($this->preg($obj->message->attributes->from)),
                    )
                );
            }
        }

    }
    return $uniqueArray;
}

private function preg($value)
{
    return preg_replace($this->patterns, $this->replace, $value);
}

protected function fuse($input, $combine)
{
    $output = array();
    foreach ($input AS $key => &$value)
    {
        $output[$key] = $value;

        $flip = array_flip($input[$key]);

        if(! isset($flip[$combine[$key][0]])) $output[$key][] = $combine[$key][0];
    }
    return $output;
}
于 2013-07-25T17:01:11.233 回答