PHP-foreach在每个新创建的数组中重复值

时间:2018-09-04 02:42:55

标签: php arrays

我正在循环array,并尝试使用element获取并推入每个数组中的每个associative key。然后将其再次推入另一个Row数组中。该循环不应将前一个element推入下一个Row数组。但是它一直重复到循环结束

我得到的输出。

Array
(
    [Row1] => Array
        (
            [Container1] => Container is empty at row 1
        )

    [Row2] => Array
        (
            [Container1] => Container is empty at row 1
            [Container2] => Container is empty at row 2
        )

    [Row4] => Array
        (
            [Container1] => Container is empty at row 1
            [Container2] => Container is empty at row 2
            [Container4] => Container is empty at row 4
        )

    [Row5] => Array
        (
            [Container1] => Container is empty at row 1
            [Container2] => Container is empty at row 2
            [Container4] => Container is empty at row 4
            [Container5] => Container is empty at row 5
        )

    [Row6] => Array
        (
            [Container1] => Container is empty at row 1
            [Container2] => Container is empty at row 2
            [Container4] => Container is empty at row 4
            [Container5] => Container is empty at row 5
            [Container6] => Container is empty at row 6
        )

我需要的输出。

Array
    (
        [Row1] => Array
            (
                [Container1] => Container is empty at row 1
            )

        [Row2] => Array
            (
                [Container2] => Container is empty at row 2
            )

        [Row4] => Array
            (
                [Container4] => Container is empty at row 4
            )

        [Row5] => Array
            (
                [Container5] => Container is empty at row 5
            )

        [Row6] => Array
            (
                [Container6] => Container is empty at row 6
            )

下面是我的for循环

<?php

$errorArray = array();
$ContainerError = array();

for ($i = 1; $i < count($namedDataArray); $i++) { 
    if ($namedDataArray[$i][1] == ''){
        echo '<pre>';
        $ContainerError['Container'.$i] = 'Container is empty at row '. $i;
        echo '</pre>';
        $errorArray['Row'.$i] = $ContainerError;
    }
}

3 个答案:

答案 0 :(得分:2)

我是通过在$ContainerError = array();的结尾处再次初始化if statement来实现的

答案 1 :(得分:1)

您可以在每个循环的最后重新初始化$ContainerError,这样就可以正常工作。 另一种可行的方法是,如果循环足够大,则效率会更高一些,那就是在进入循环之前先初始化一次数组,然后在每个循环结束时使用unset($ContainerError);

对其进行取消设置

效果是完全一样的,但是在过去,当循环进入数万次迭代时,我发现这样做可以节省一到两秒钟的执行时间

答案 2 :(得分:1)

一个简单的解决方案,您不需要$ContainerError

<?php
array_walk($namedDataArray, function($row,$index) use (&$errorArray){
    static $i=0; // You can use $index instead of $i
    $errorArray['Row'.$i]['Container'.$i] = 'Container is empty at row '. $i++;
});

测试:

<?php

$errorArray = array();
$ContainerError = array();


$namedDataArray = range(0, 6);

array_walk($namedDataArray, function() use (&$errorArray){
    static $i=0;
    $errorArray['Row'.$i]['Container'.$i] = 'Container is empty at row '. $i++;
});


print_r( $errorArray );

显示:

Array
(
    [Row0] => Array
        (
            [Container0] => Container is empty at row 
        )

    [Row1] => Array
        (
            [Container1] => Container is empty at row 1
        )

    [Row2] => Array
        (
            [Container2] => Container is empty at row 2
        )

    [Row3] => Array
        (
            [Container3] => Container is empty at row 3
        )

    [Row4] => Array
        (
            [Container4] => Container is empty at row 4
        )

    [Row5] => Array
        (
            [Container5] => Container is empty at row 5
        )

    [Row6] => Array
        (
            [Container6] => Container is empty at row 6
        )

)