<?php
$rightSide = array(15, 15, 15, 15, 18, 18, 19, 21, 21, 21, 25, 25);
$leftSide = array(13, 14, 12, 11, 16, 17, 20, 22, 23, 24, 30, 31);
$rightFlip = array_flip($rightSide);
for($k=0; $k<count($rightFlip); $k++)
{
$arr25[$k] = $k;
}
$array25 = array_combine($arr25, $rightFlip);
$k = 0;
while($k<count($array25))
{
if($k==0)
{
$finArr[0] = $rightSide[0];
$repeat_0 = $array25[0] - (-1);
echo "Repeat_0 = " . $repeat_0. "</br>";
for($i=0; $i<$repeat_0; $i++)
{
$finArr[$i+1] = $leftSide[$i];
}
}
/*
else if($k==1)
{
$repeat = $array25[$k] - $array25[$k-1];
$repeatPrev = $array25[$k-1] - $repeat[$k-2];
$insideUsed = $repeat_0;
$finArr[$i+1] = $rightSide[$repeat_0];
for($j=$i+2; $j<($repeat+$i+2); $j++)
{
$finArr[$j] = $leftSide[$insideUsed];
$insideUsed = $insideUsed + 1;
}
}
else if($k==2)
{
$repeat = $array25[$k] - $array25[$k-1];
$repeatPrev = $array25[$k-1] - $array25[$k-2];
echo "Repeat = " . $repeat. "</br>";
echo "RepeatPrev = " . $repeatPrev. "</br>";
$finArr[$j] = $rightSide[$repeat_0 + $repeatPrev];
$insideUsed = $repeat_0 + $repeatPrev;
for($l=$j+1; $l<($repeat+$j+1); $l++)
{
$finArr[$l] = $leftSide[$insideUsed];
$insideUsed = $insideUsed + 1;
}
}
/* else if($k==3)
{
$repeat = $array25[$k] - $array25[$k-1];
$repeatPrev = $array25[$k-1] - $array25[$k-2];
$repeatPrevPrev = $array25[$k-2] - $array25[$k-3];
$finArr[$l] = $rightSide[$repeat_0 + $repeatPrevPrev + $repeatPrev];
$insideUsed = $repeat_0 + $repeatPrevPrev + $repeatPrev;
for($m=$l+1; $m<($repeat+$l+1); $m++)
{
$finArr[$m] = $leftSide[$insideUsed];
$insideUsed = $insideUsed + 1;
}
}*/
else
{
echo "</br> for k = " . $k . "</br>";
for($n=$k; $n>0; $n--)
{
$repeat_arr[$n] = $array25[$n] - $array25[$n-1];
echo "Repeat_arr[". $n . "] = ". $repeat_arr[$n];
}
$repeat_arr[0] = $array25[0] - (-1);
echo "Repeat_arr 0 = " . $repeat_arr[0];
for($I=0; $I<$k; $I++)
{
$addRepeats[$k] += $repeat_arr[$I];
}
$addRepeats[$k] += $k;
echo "addRepeats = " . $addRepeats[$k];
for($I=0; $I<$k; $I++)
{
$added[$k] += $repeat_arr[$I];
}
$finArr[$addRepeats[$k]] = $rightSide[$added[$k]];
for($I=$addRepeats[$k]+1; $I<($repeat_arr[$k] + $addRepeats[$k] + 1); $I++)
{
$finArr[$I] = $leftSide[$added[$k]];
$added[$k] = $added[$k] + 1;
}
}
$k++;
}
echo "<table>";
echo "<tr>";
echo "<th>Array25</th>";
echo "<th>RightSide</th>";
echo "<th>LeftSide</th>";
echo "<th>finArr</th>";
echo "</tr>";
echo "<tr>";
echo "<td>";
echo "<pre>";
print_r($array25);
echo "</pre>";
echo "</td>";
echo "<td>";
echo "<pre>";
print_r($rightSide);
echo "</pre>";
echo "</td>";
echo "<td>";
echo "<pre>";
print_r($leftSide);
echo "</pre>";
echo "</td>";
echo "<td>";
echo "<pre>";
print_r($finArr);
echo "</pre>";
echo "</td>";
echo "</tr>";
echo "</table>";
This answer will be correct no matter how many elements are added or subtracted from rightside array and corresponding elements added / subtracted from left side array.. to make changes in array25 array……
solved create an array based on the values in 2 existing arrays