我有2个数组: -
Array ( [0] => 15
[1] => 15
[2] => 18
[3] => 18
[4] => 19
[5] => 21
[6] => 21
[7] => 21 )
Array ( [0] => 13
[1] => 14
[2] => 16
[3] => 17
[4] => 20
[5] => 22
[6] => 23
[7] => 24 )
我想要第三个数组: -
Array ( [0] => 15
[1] => 13
[2] => 14
[3] => 18
[4] => 16
[5] => 17
[6] => 19
[7] => 20
[8] => 21
[9] => 22
[10] => 23
[11] => 24 )
3个数组之间的关系是这样的,第一个数组元素必须是第三个数组的第一个元素,然后第一个元素在第一个数组中恰好出现2次,所以第三个数组的下两个元素将是从第二个阵列中拾取。请指导?
答案 0 :(得分:0)
您可以尝试这样:
$f1 = array ( 0 => 15 ,
1 => 15 ,
2 => 18 ,
3 => 18 ,
4 => 19,
5 => 21 ,
6 => 21,
7 => 21 );
$f2 = array ( 0 => 13,
1 => 14 ,
2 => 16 ,
3 => 17 ,
4 => 20 ,
5 => 22 ,
6 => 23 ,
7 => 24 ) ;
$result = array_merge((array)$f1, (array)$f2);
$input = array_unique($result);
print_r($input);
答案 1 :(得分:0)
使用array_combine()或array_merge然后使用array_flip()函数..如果值是唯一的,则使用array_unique()
您必须使用array_flip()将值更改为键。
答案 2 :(得分:0)
<?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>";
无论从右侧数组中添加或减去多少元素以及从左侧数组中添加/减去相应的元素,这个答案都是正确的...来对array25数组进行更改......