PHP: comparing arrays for values
$a1 = array('A', 'A', 'A', 'A', 'B', 'C');
$a2 = array('A', 'A', 'A', 'A', 'B', 'C');
I want to match the first 'A'
in $a1
with the last 'A'
in $a2
. And the second 'A'
in $a1
with the second-from-last 'A'
in $a2
.
$a1
with $a2
, like so:
0 => 'A' with 3 => 'A'
1 => 'A' with 2 => 'A'
2 => 'A' with 1 => 'A'
3 => 'A' with 0 => 'A'
I need an if statement to determine if there is another insta开发者_运维问答nce of 'A'
in $a1
to match it with its respective value in $a2
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);
In case you just want to match 'A' in the reverse order that you described, you can try this:
$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--;
}
}
My suggestion:
<?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;
}
?>
Outputs:
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
This do exactly what you explain. Its less complete but It compares both arrays as you explain and is more compact.
$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";
}
精彩评论