2013-07-24 59 views
2

我已閱讀並試圖將幾乎所有的remove從嵌套數組線程是在那裏重複,我相信這個問題是,我試圖刪除整個輕微的獨特從一個(非常)大的多維數組中複製分支。我想這是更多的從數組中刪除重複數組類型的問題?刪除重複嵌套數組

我這裏有上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; 

這是我的輸出的一個切片的口是心非的例子陣列。

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

爲什麼'//跳過it'?刪除它 – vladkras

+0

您可以這樣做:http://stackoverflow.com/a/946300/945775 – AgmLauncher

+0

可能重複[如何從PHP中的多維數組中刪除重複的值](http://stackoverflow.com/questions/307674/how-to-remove-duplicate-values-from-a-multi-dimensional-array-in-php) – AgmLauncher

回答

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; 
} 
+0

如果這種解決方案適合你,那麼你應該接受它。 –

+0

@JimMartens我正在等待超時時間來接受我自己的答案,並已經忘記了它= / – ehime