2014-01-17 5 views
1

말할 수 있습니다이 내 SQL 데이터베이스 컬럼 값이다, 나는 다음과 같은 배열을 가지고Array_intersect() 값을 다른 배열의 해당 키와 비교하는 방법은 무엇입니까?

Result -  Reference 

    1    A1 
    2    A2 
    3    A3 
    4    A4 
    5    A5 

위의 열을 가져 오는 후 :

: 나는 array_intersect($inputValue, $result);을 수행 할 때

$inputValue = array(1,3,5); //This is the User Input 

$result = array(1,2,3,4,5); //This is the mysql fetched result of 'Results' column 

$reference = array('A1','A2','A3','A4','A5'); //This is the fetched result of 'Reference' column 

$filteredData = array_intersect($inputValue, $result); 

print_r($filteredData); 

이 출력을 제공합니다

Array ([0] => 1 
    [1] => 3 
    [2] => 5) 

이제 array_intersect 이후에 해당 참조를 다른 배열에 어떻게 저장합니까? 즉, 교차 후 $filteredData의 배열 값은 1,3,5입니다. 이제는 해당 참조가이 값을 가져야하는 별도의 배열에 저장되기를 바랍니다. $MatchingReference = array(A1,A3,A5);

어떻게 수행됩니까?

답변

1

연관 배열에 데이터베이스에서 두 배열을 결합합니다. 그런 다음 사용자 입력을 사용하여 키의 하위 집합 만 선택하십시오.

$filteredData = array_intersect_key(
    array_combine($result, $reference), 
    array_flip($inputValue) 
); 
+0

당신의 방법도 효과가 있습니다. 너와 Xardas 제안은 내가 원했던대로 정확히 수행된다. 환상적 :) – Neel

+0

몇 가지 테스트를 거친 후, 귀하의 방법이 최선의 방법이라고 생각합니다. @Xardas가 답을 제시 할 때 내 이유에 덧붙였다. :) 감사합니다 빌. – Neel

2

나는 이것을 시도 할 것이다 :

$filteredReferences = array(); 
foreach($filteredData as $key) { 
    $filteredReferences[] = $reference[$key-1]; 
} 
+0

나는 당신의 제안을 좋아한다. 그러나 나는 그것을 얻었습니다 :'Notice : Undefined offset : 라인 18에있는 C : \ Users \ test.php의 5 : 배열 ([0] => A2 [1] => A4 [2] =>) '. 내 라인 18은'$ filteredReferences [] = $ reference [$ key];' – Neel

+2

배열 색인은 1이 아닌 0에서 시작한다는 것을 기억하십시오. –

+0

답변 고정 ... – Xardas