2013-07-24 1 views
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; 

내 출력 슬라이스 인 배열에서 이중성의 예 정렬.

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

왜 '// 건너 뛰세요'? 그것을 삭제하십시오 – 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