2011-02-01 2 views
0
$a1 = array('A', 'A', 'A', 'A', 'B', 'C'); 
$a2 = array('A', 'A', 'A', 'A', 'B', 'C'); 

에 대한 비교 배열 나는 $a2의 마지막 'A'$a1의 첫 'A' 일치하도록합니다. 그리고 두 번째는 의 $a1이고 의 마지막 두 번째'A'$a2입니다. $a2PHP : 값

$a1,과 같이 : if 문

0 => 'A' with 3 => 'A' 
1 => 'A' with 2 => 'A' 
2 => 'A' with 1 => 'A' 
3 => 'A' with 0 => 'A' 

나는 'A'의 다른 인스턴스가 $a2

답변

0
function myMatch($vala, $valb) { 
/*this can make your custom matching equality, length in case of strings, types what ever 
returns true if values $vala and $valb match and false if they do not 
*/ 
} 

$matches = array(); 

foreach($a1 as $key=>$value) { 

    if(isset($a2[$key])) { 

    $matches[$key] = myMatch($a1[$key],$a2[$key]) ? 
"matches for ".$key." with value ".$value : "no match for ".$key." with ".$value; 

    } else { 

     $matches[$key] = "Missing pair in a2 for key ".$key; 

    } 

} 


print_R($matches); 
0

에 각각의 값과 일치하도록 $a1에서이 있는지 확인하기 위해 필요 앞에서 설명한 것과 반대 순서로 'A'와 일치 시키려면 다음을 시도하십시오.

$a1 = array('A', 'A', 'A', 'A', 'B', 'C'); 
$a2 = array('A', 'A', 'A', 'A', 'B', 'C'); 

$j=count($a2)-1; 
for($i=0;$i<count($a1);$i++){ 
    while($j>=0){ 
     if($a2[$j]=='A'){ 
      echo "Match ($i,$j)"; 
      $j--; 
      break; 
     } 
     else 
      $j--; 
    } 
} 
0

나의 제안 :

<?php 

$a1 = array('A', 'A', 'A', 'A', 'B', 'C'); 
$a2 = array('A', 'A', 'A', 'A', 'B', 'C'); 

$a3 = getInversedArrayIndexMatch($a1, $a2); 
print_r($a3); 

if(getInversedArrayIndexMatch($a1, $a2, 3)) { 
    echo 'Value with index 3 exists in a2'; 
} 

/** Function **/ 
function getInversedArrayIndexMatch($a1, $a2, $indexSearch = null) 
{ 
    $arrWithInversedMatches = array(); 
    $inversedArray = array_reverse($a2); 
    ksort($inversedArray); 

    $len1 = count($a1)-1; 
    $len2 = count($a2)-1; 
    for($i=0; $i<=$len1; $i++) { 
     $valNeedle = $a1[$i]; 

     $foundKeys = array_keys($inversedArray, $valNeedle); 
     if(is_array($foundKeys) && count($foundKeys) > 0) { 
      $valueIndex = count($foundKeys)-1; 
      $inversedIndex = $foundKeys[$valueIndex]; 
      $removeIndex = array_search($valNeedle, $inversedArray); 
      array_splice($inversedArray, $removeIndex, 1, ""); 
      if(!isset($indexSearch)) { 
       $arrWithInversedMatches[] = "Index $i with value $valNeedle in Array1 matches the $valueIndex. $valNeedle in inversed Array2 at index ". ($len2 - $removeIndex) .""; 
      } elseif ($indexSearch == $i) { 
       return true; 
      }   
     } else { 
      $arrWithInversedMatches[] = "Index $i with value $valNeedle in Array1 has no match in inversed Array2"; 
     } 
    } 
    if(isset($indexSearch)) { 
     return false;   
    } 
    return $arrWithInversedMatches; 
} 

>

출력 :

Array 
(
    [0] => Index 0 with value A in Array1 matches the 3. A in inversed Array2 at index 3 
    [1] => Index 1 with value A in Array1 matches the 2. A in inversed Array2 at index 2 
    [2] => Index 2 with value A in Array1 matches the 1. A in inversed Array2 at index 1 
    [3] => Index 3 with value A in Array1 matches the 0. A in inversed Array2 at index 0 
    [4] => Index 4 with value B in Array1 matches the 0. B in inversed Array2 at index 4 
    [5] => Index 5 with value C in Array1 matches the 0. C in inversed Array2 at index 5 
) 

Value with index 3 exists in a2 
0

이 정확히 설명 무엇. 그것의 덜 완성되었지만 당신이 설명하고 더 작고 두 배열을 비교합니다.

$a1 = array('A', 'A', 'A', 'A', 'B', 'C'); 
$a2 = array('A', 'A', 'A', 'A', 'B', 'C'); 

$a2_aux = array_reverse($a2); 
//reset internal pointer 
reset($a1); 
reset($a2); 

while (list($pos_a1, $value_a1) = each($a1)) 
{ 
    list($pos_a2, $value_a2) = each($a2_aux); 

    if($value_a1 == $value_a2) 
     echo 'At position '.$pos_a1. ' the value '.$value_a1.' in a1 array, is in a2 array. Because'.$value_a1. '='.$value_a2."\n" ; 
    else 
     echo 'At position '.$pos_a1. ' the value '.$value_a1.' in a1 array, is NOT in a2 array. Because '.$value_a1. '!='.$value_a2."\n"; 
}