user1687891
user1687891

Reputation: 864

How to handle Undefined offset error for odd array items?

I have an array with 13 items as below:

$skus = array(
            "PK 800G",
            "CH 800G",
            "910G",
            "400G",
            "1.5KG",
            "1KG",
            "A 1KG",
            "E 1KG",
            "D 1KG",
            "2 G.",
            "ESSENCE",
            "N 800G.",
            "H 800 G"
);

My problem is if I have 13 items in the array, following code give me error:

(1/1) ErrorException
Undefined offset: 13

But if I remove single item from the array and make it "12" then it works fine. I am sure it is because of the loop. But I am not able to figure it out.

$total = count($skus);
$skuKeys = array_keys($skus);
$iter = ceil($total / 4);

for ($n = 0; $n <= $iter; $n++) {
    $row = ["G", "A"];
    $row2 = ["", ""];
    for ($j = $n*4; $j < 4 * ($n+1); $j++) {
        if ($j == 2) {
            $row = array_merge($row, ["RT + PK","","","","","",""]);
            $row2 = array_merge($row2, ["TY", "LY", "YTD", "TY", "LY", "MTD", ""]);
        }
        $row = array_merge($row, [$skuKeys[$j],"","","","","",""]);
        $row2 = array_merge($row2, ["TY", "LY", "YTD", "TY", "LY", "MTD", ""]);
    }
}

Can anybody please help me?

Thank You.

Upvotes: 1

Views: 63

Answers (3)

Nick
Nick

Reputation: 147206

You have a couple of problems. First, in your computation of $iter, you need to use

$iter = floor(($total - 1) / 4);

otherwise for a $total which is a multiple of 4 you will do one too many iterations through the loop, and since you are using $n <= $iter in your outer for loop you need to use floor instead of ceil, otherwise you will again do one too many iterations through the loop.

Your second issue (and the one causing the error message) is that in your inner for loop you are not checking that you are still within the bounds of the array. So after the for, you need to add a check for that:

if ($j == $total) break;

So your code should change to:

$iter = floor(($total - 1) / 4);
for ($n = 0; $n <= $iter; $n++) {
    $row = ["G", "A"];
    $row2 = ["", ""];
    for ($j = $n*4; $j < 4 * ($n+1); $j++) {
        if ($j == $total) break;
        if ($j == 2) {
            $row = array_merge($row, ["RT + PK","","","","","",""]);
            $row2 = array_merge($row2, ["TY", "LY", "YTD", "TY", "LY", "MTD", ""]);
        }
        $row = array_merge($row, [$skuKeys[$j],"","","","","",""]);
        $row2 = array_merge($row2, ["TY", "LY", "YTD", "TY", "LY", "MTD", ""]);
    }
}

I suspect that you have other issues within your code (unless you are doing something with $row and $row2 in the loop that you haven't shown us), as you are overwriting the values of $row and $row2 on each pass through the outer loop. However these fixes will resolve your current issue.

Upvotes: 1

Artee
Artee

Reputation: 834

Try this:

$skus = array(
            "PK 800G",
            "CH 800G",
            "910G",
            "400G",
            "1.5KG",
            "1KG",
            "A 1KG",
            "E 1KG",
            "D 1KG",
            "2 G.",
            "ESSENCE",
            "N 800G.",
            "H 800 G"
);

$total = count($skus);
$skuKeys = array_keys($skus);
$iter = round($total / 4);

for ($n = 0; $n < $iter; $n++) {

    $row = ["G", "A"];
    $row2 = ["", ""];
    for ($j = $n*4; $j < 4 * ($n+1); $j++) {
        if ($j == 2) {
            $row = array_merge($row, ["RT + PK","","","","","",""]);
            $row2 = array_merge($row2, ["TY", "LY", "YTD", "TY", "LY", "MTD", ""]);
        }
        $row = array_merge($row, [$skuKeys[$j],"","","","","",""]);
        $row2 = array_merge($row2, ["TY", "LY", "YTD", "TY", "LY", "MTD", ""]);
    }

}

var_dump($row);
var_dump($row2);

Upvotes: 1

Twinkle
Twinkle

Reputation: 189

Actually, you are working on index of an array which is 1 less then the length of the array. TRY ...

$total = count($skus);
$skuKeys = array_keys($skus);
$iter = ceil($total / 4);

for ($n = 0; $n < $iter; $n++) {
    $row = ["G", "A"];
    $row2 = ["", ""];
    for ($j = $n*4; $j < 4 * ($n+1); $j++) {
        if ($j == 2) {
            $row = array_merge($row, ["RT + PK","","","","","",""]);
            $row2 = array_merge($row2, ["TY", "LY", "YTD", "TY", "LY", "MTD", ""]);
        }
        $row = array_merge($row, [$skuKeys[$j],"","","","","",""]);
        $row2 = array_merge($row2, ["TY", "LY", "YTD", "TY", "LY", "MTD", ""]);
    }
}

Upvotes: 1

Related Questions