我已阅读并尝试将几乎所有删除重复的值从嵌套数组线程中应用到那里,我相信这个问题在这方面有点独特我试图从(非常)大的多维数组中删除整个重复的分支。我想这是一个从数组类型的问题中移除重复数组?
我在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;
}
这是运行它的代码AS,以及数组中双重性的一个例子
$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
答案 0 :(得分:0)
结束时,没有任何重复,到和来自字段不一样。
我提出的解决方案是删除并重新添加消息属性,这些消息属性将这些字段从程序逻辑中取出,然后通过将删除的哈希值与当前键匹配来重新连接它们。干杯,希望这有助于某人。
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;
}