0

I'm looping in an array and trying to get and push each element in another array with associative key. then push it again in another array that serves as a Row array. The loop shouldn't push the previous element on the next Row array. But it keeps repeating till the end of the loop

Output that I am getting.

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
        )

The output that I need to.

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
            )

Below is my for loop

<?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 Answers 3

2

I got it by initializing again $ContainerError = array(); at the end of if statement

Sign up to request clarification or add additional context in comments.

Comments

1

You could re-initialize $ContainerError at the very end of each loop as you did and it'll work fine. Another way to go, a little bit more efficient if the loop is big enough, is to initialize the array once before entering the loop and then unsetting it at the end of each loop with unset($ContainerError);

The effect is exactly the same, but in the past I've found this to shave a second or two of execution time when the loop goes into the tens of thousands of iterations

1 Comment

I'll take note for that unsetting array for better execution time. Thanks!
1

A simple and easy solution, you don't need $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++;
});

Test:

<?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 );

Shows:

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
        )

)

1 Comment

Good to know about array_walk function. I'll try this in my future projects. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.