Codice PHP:
<?php
function subArr($array, $lig, $col, $x, $y) {
$arrRet = array();
$ii = 0;
for ($i=$lig;$i<=$lig+$x;$i++) {
$jj = 0;
for ($j=$col;$j<=$y+$col;$j++) {
$arrRet[$ii][$jj] = $array[$i][$j];
$jj++;
} // for ($j=0;$j<=$y;$i++)
$ii++;
} // for ($i=$col;$i<=$x+$col;$i++)
return $arrRet;
} // function copyArr($array, $col, $x, $y)
function copyArr($array, $lig, $col, $arrayToCopy, $x, $y) {
$arrayRet = $array;
$ii = 0;
for ($i=$lig;$i<=$lig+$x;$i++) {
$jj = 0;
for ($j=$col;$j<=$y+$col;$j++) {
$arrayRet[$i][$j] = $arrayToCopy[$ii][$jj];
$jj++;
} // for ($j=0;$j<=$y;$i++)
$ii++;
} // for ($i=$col;$i<=$x+$col;$i++)
return $arrayRet;
} // function copyArr($array, $col, $x, $y)
function getArrayDepth($array) {
$i = 0;
foreach ($array as $value) {
$i++;
}
return $i;
}
function multidim_find($biggestArray,$smallestArray) {
$rowsBA = count($biggestArray);
$depthBA = getArrayDepth($biggestArray[0]);
$rowsSA = count($smallestArray);
$depthSA = getArrayDepth($smallestArray[0]);
for ($i=0;$i<=$rowsBA-1-$rowsSA+1;$i++) {
for ($j=0;$j<=$depthBA-1-$depthSA+1;$j++) {
if ($smallestArray == subArr($biggestArray,$i,$j,$rowsSA-1,$depthSA-1)) {
return true;
} // if ($smallestArray == copyArr($a,$i,$j,rowsSA-1,$depthSA-1))
} // for ($j=0;$j<=$depthBA-1-$depthSA+1;$j++)
} // for ($i=0;$i<=$rowsBA-1-$rowsSA+1;$i++)
return false;
} // multidim_find($biggestArray,$smallestArray)
function multidim_replace($biggestArray,$smallestArray,$replaceArray) {
$rowsBA = count($biggestArray);
$depthBA = getArrayDepth($biggestArray[0]);
$rowsSA = count($smallestArray);
$depthSA = getArrayDepth($smallestArray[0]);
for ($i=0;$i<=$rowsBA-1-$rowsSA+1;$i++) {
for ($j=0;$j<=$depthBA-1-$depthSA+1;$j++) {
if ($smallestArray == subArr($biggestArray,$i,$j,$rowsSA-1,$depthSA-1)) {
return copyArr($biggestArray,$i,$j,$replaceArray,$rowsSA-1,$depthSA-1);
} // if ($smallestArray == copyArr($a,$i,$j,rowsSA-1,$depthSA-1))
} // for ($j=0;$j<=$depthBA-1-$depthSA+1;$j++)
} // for ($i=0;$i<=$rowsBA-1-$rowsSA+1;$i++)
return false;
} // multidim_replace($biggestArray,$smallestArray,$replaceArray)
$a[0] = array(0,0,1,0,0,0,0,0);
$a[1] = array(0,1,0,0,0,0,0,1);
$a[2] = array(0,0,0,0,0,0,0,0);
$a[3] = array(0,0,1,1,1,0,0,0);
$a[4] = array(0,0,1,0,1,0,0,0);
$a[5] = array(0,0,1,1,1,0,0,0);
$a[6] = array(0,0,0,0,0,0,0,0);
$a[7] = array(1,0,0,1,0,0,0,0);
$toFind[0] = array(1,1,1);
$toFind[1] = array(1,0,1);
$toFind[2] = array(1,1,1);
$toReplace[0] = array(2,2,2);
$toReplace[1] = array(2,0,2);
$toReplace[2] = array(2,2,2);
if (multidim_find($a,$toFind)) {
print "Trovato";
} else {
print "Non trovato";
} // if (multidim_find($a,$toFind))
print "<br/>";
print "<strong>Prima :</strong><br/>";
foreach ($a as $key => $avalue) {
foreach ($avalue as $value) {
print "[$value]";
}
print "<br/>";
}
//---- Rimpiazzamento
$myResult = multidim_replace($a,$toFind,$toReplace);
print "<strong>Dopo :</strong><br/>";
foreach ($myResult as $key => $avalue) {
foreach ($avalue as $value) {
print "[$value]";
}
print "<br/>";
}
?>