我有2个阵列。
我想在array1中的每个元素之后添加array2中的每个元素。
你能帮我解决一下如何用PHP来解决这个问题。
Array1[] = ("Test1","Test2","Test3","Test4");
Array2[] = ("ADD1","ADD2","ADD3","ADD4");
FinalArray[] =("Test1","ADD1","Test2","ADD2","Test3","ADD3","Test4","ADD4");
感谢。
答案 0 :(得分:2)
您可以使用foreach
$Array1 = array("Test1","Test2","Test3","Test4");
$Array2 = array("ADD1","ADD2","ADD3","ADD4");
foreach ($Array1 as $key => $value) {
$FinalArray[] = $value;
if(isset($Array2[$key]))
$FinalArray[] = $Array2[$key];
}
print_r($FinalArray);
答案 1 :(得分:1)
试试这个
<?php
$Array1 = array("Test1","Test2","Test3","Test4");
$Array2 = array("ADD1","ADD2","ADD3","ADD4");
for($i=0 ;$i<count($Array1);$i++ ){
$a[] = $Array1[$i];
$a[] = $Array2[$i];
}
print_r($a);
?>
答案 2 :(得分:0)
尝试使用此代码:
<?php
$arr1 = array("Test1","Test2","Test3","Test4");
$arr2 = array("ADD1","ADD2","ADD3","ADD4");
$final_arr = array();
$index1=0;
$index2=0;
for($i=1;$i<=count($arr1)+count($arr2);$i++)
{
if($i%2==0)
{
array_push($final_arr,$arr2[$index2]);
$index2++;
}
else
{
array_push($final_arr,$arr1[$index1]);
$index1++;
}
}
答案 3 :(得分:0)
试一试:
$k=0;
$j=0;
$final_arr = array();
for($i=1;$i<=count($arr1);$i++)
{
if($i%2!=0)
{
array_push($final_arr,$arr1[$k]);
$k++;
}
else
{
array_push($final_arr,$arr2[$j]);
$j++;
}
}