Php 从多个循环向数组添加项

Php 从多个循环向数组添加项,php,for-loop,foreach,Php,For Loop,Foreach,下面提供的代码是我真实代码的摘要。我想使用多个循环将多个数组中的项添加到最终数组中。但我还想多次添加相同的值,从而将所有内容包装到另一个循环中。这听起来很混乱,但我认为代码解释得很好。为什么我只能从一个循环中得到结果?换句话说,为什么$total只包含六个元素,而不是像for循环所期望的那样包含30个元素(一个六个五次) for ($counter = 1; $counter < 5; $counter++) { $first_arr = array('one', 'two',

下面提供的代码是我真实代码的摘要。我想使用多个循环将多个数组中的项添加到最终数组中。但我还想多次添加相同的值,从而将所有内容包装到另一个循环中。这听起来很混乱,但我认为代码解释得很好。为什么我只能从一个循环中得到结果?换句话说,为什么
$total
只包含六个元素,而不是像for循环所期望的那样包含30个元素(一个六个五次)

for ($counter = 1; $counter < 5; $counter++) {
     $first_arr = array('one', 'two', 'three');
     $second_arr = array('four', 'five', 'six');

    $total = array();

    foreach ($first_arr as $x) {
        $total[] = $x;
    }

    foreach ($second_arr as $x) {
        $total[] = $x;
    }
}
var_dump($total);
($counter=1;$counter<5;$counter++)的
{
$first_arr=数组('1','2','3');
$second_arr=数组('four','five','six');
$total=array();
foreach(首付为$x){
$total[]=$x;
}
foreach(第二次租金为$x){
$total[]=$x;
}
}
var_dump(总计);

因为您在外部循环的每次迭代中都要重置
$total
。在外循环之前声明它,您的问题就会得到解决

像这样:

$total = array();
$first_arr = array('one', 'two', 'three');
$second_arr = array('four', 'five', 'six');

for ($counter = 1; $counter < 5; $counter++) {
    // assuming $first_arr and $second_arr have numerical keys
    // which they do, in this example
    $total = array_merge($total, $first_arr, $second_arr);
}

var_dump($total);
$total=array();
$first_arr=数组('1','2','3');
$second_arr=数组('four','five','six');
对于($counter=1;$counter<5;$counter++){
//假设$first_arr和$second_arr有数字键
//在这个例子中,他们是这样做的
$total=数组合并($total、$first\u arr、$second\u arr);
}
var_dump(总计);

因为您在外部循环的每次迭代中都要重置
$total
。在外循环之前声明它,您的问题就会得到解决

像这样:

$total = array();
$first_arr = array('one', 'two', 'three');
$second_arr = array('four', 'five', 'six');

for ($counter = 1; $counter < 5; $counter++) {
    // assuming $first_arr and $second_arr have numerical keys
    // which they do, in this example
    $total = array_merge($total, $first_arr, $second_arr);
}

var_dump($total);
$total=array();
$first_arr=数组('1','2','3');
$second_arr=数组('four','five','six');
对于($counter=1;$counter<5;$counter++){
//假设$first_arr和$second_arr有数字键
//在这个例子中,他们是这样做的
$total=数组合并($total、$first\u arr、$second\u arr);
}
var_dump(总计);

将以下行置于循环外部

 $total = array();

将以下行置于循环外部

 $total = array();

只要做一些改变……它就会起作用

for ($counter = 1; $counter < 5; $counter++) {
 $first_arr = array('one', 'two', 'three');
 $second_arr = array('four', 'five', 'six');

$total = array();
$i = 0;

foreach ($first_arr as $x) {
    $total[$i] = $x;
    $i++;
}

foreach ($second_arr as $x) {
    $total[$i] = $x;
    $i++;
}
}
var_dump($total);
($counter=1;$counter<5;$counter++)的
{
$first_arr=数组('1','2','3');
$second_arr=数组('four','five','six');
$total=array();
$i=0;
foreach(首付为$x){
$total[$i]=$x;
$i++;
}
foreach(第二次租金为$x){
$total[$i]=$x;
$i++;
}
}
var_dump(总计);

希望它能有所帮助。

只需做一些改变……它就会起作用

for ($counter = 1; $counter < 5; $counter++) {
 $first_arr = array('one', 'two', 'three');
 $second_arr = array('four', 'five', 'six');

$total = array();
$i = 0;

foreach ($first_arr as $x) {
    $total[$i] = $x;
    $i++;
}

foreach ($second_arr as $x) {
    $total[$i] = $x;
    $i++;
}
}
var_dump($total);
($counter=1;$counter<5;$counter++)的
{
$first_arr=数组('1','2','3');
$second_arr=数组('four','five','six');
$total=array();
$i=0;
foreach(首付为$x){
$total[$i]=$x;
$i++;
}
foreach(第二次租金为$x){
$total[$i]=$x;
$i++;
}
}
var_dump(总计);

希望它有帮助。

这是因为您在循环中声明了数组。每次循环再次启动时,数组都会重置为空值,因此最终的循环值将显示在结果中

在圈外发表这些言论,效果会很好

$first_arr = array('one', 'two', 'three');
$second_arr = array('four', 'five', 'six');
    for ($counter = 1; $counter <= 5; $counter++) {
        foreach ($first_arr as $x) {
            $total[] = $x;
        }


        foreach ($second_arr as $x) {
            $total[] = $x;
        }
    }
$first_arr=array('1','2','3');
$second_arr=数组('four','five','six');

对于($counter=1;$counter这是因为您在循环中声明了数组。每次循环再次启动时,数组都会用null值重置,因此最终的循环值会显示在结果中

在圈外发表这些言论,效果会很好

$first_arr = array('one', 'two', 'three');
$second_arr = array('four', 'five', 'six');
    for ($counter = 1; $counter <= 5; $counter++) {
        foreach ($first_arr as $x) {
            $total[] = $x;
        }


        foreach ($second_arr as $x) {
            $total[] = $x;
        }
    }
$first_arr=array('1','2','3');
$second_arr=数组('four','five','six');

对于($counter=1;$counter如果您的示例代表您的实际用例,如
$first\u arr
$second\u arr
$total
具有数字键,那么可以使用
数组合并
更干净、更有效地完成此操作。如果您的示例代表您的实际用例,如
$first>_arr
$second\u arr
$total
具有数字键,可以使用
array\u merge
更干净、更高效地完成此操作。