2012-11-11 3 views
0

는 그래, 난 array_unique 기능을 알고,하지만 일이 일치하는 예를 들어 내 검색어에 합법적 인 중복이있을 수 있습니다 :preg_match 출력에서 ​​실제 복제본 만 제거하는 방법은 무엇입니까?

$str = "fruit1: banana, fruit2: orange, fruit3: banana, fruit4: apple, fruit5: banana"; 
preg_match("@fruit1: (?<fruit1>\w+), fruit2: orange, fruit3: (banana), fruit4: (?<fruit4>apple), fruit5: (banana)@",$str,$match); 
array_shift($match); // I dont need whole match 
print_r($match); 

출력은 다음과 같습니다 진짜

Array 
(
    [fruit1] => banana 
    [0] => banana 
    [1] => banana 
    [fruit4] => apple 
    [2] => apple 
    [3] => banana 
) 

그래서 유일한 키 중복은 [0] [2]하지만 array_unique 준다 :

는 I가 깨달았다
Array 
(
    [fruit1] => banana 
    [fruit4] => apple 
) 
+1

왜에만 [0] [2] 중복? – GBD

+0

정규식에 이름이있는 범위가 있기 때문에 명명 된 범위와 정수는 하나씩 있지만 이름이없는 범위는 정수 키만 제공합니다. 이름이 지정된 범위와 명명되지 않은 범위는 순수한 기회로 동일한 가치를 가질 수 있습니다. 따라서 array_unique를 사용하는 것은 안전하지 않습니다. – rsk82

+0

왜'0'과'1'만이 중복되는지 이해가되지 않습니다.'3'은 어떨까요? ??? – Baba

답변

1

, 용액 subseq를 삭제하는 동안의 루프 uent 키는에 하나가없는 수치입니다 :

while (next($match) !== false) { 
    if (!is_int(key($match))) { 
    next($match); 
    unset($m[key($match)]); 
    } 
} 
reset($match); 
2

이이 문제에 대한 내 솔루션입니다 :

unset($matches[0]); 
$matches = array_unique($matches); 
관련 문제