Мое предложение:
<?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