$a1 = array('A', 'A', 'A', 'A', 'B', 'C');
$a2 = array('A', 'A', 'A', 'A', 'B', 'C');
我想将'A'
中的第一个$a1
与'A'
中的最后一个$a2
相匹配。 'A'
中的第二个$a1
,'A'
中的来自后一个 $a2
。
$a1
$a2
与0 => 'A' with 3 => 'A'
1 => 'A' with 2 => 'A'
2 => 'A' with 1 => 'A'
3 => 'A' with 0 => 'A'
一样:
'A'
我需要一个if语句来确定$a1
中是否有另一个$a2
实例,以便将其与{{1}}中的相应值相匹配
答案 0 :(得分: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);
答案 1 :(得分:0)
如果你只想按照你所描述的相反顺序匹配'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--;
}
}
答案 2 :(得分: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;
}
&GT;
输出:
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
答案 3 :(得分: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";
}