4

I am trying this loop, but i have a problem in $m[2][3]. None value is showed. So i have some problem with increments.

$nArr = array('A', 'B', 'C', 'D', 'E', 'F');

$lp = 4;

for ($i = 0; $i < $lp; $i++) {
    $m[$i][$i] = 1;
    for ($x = $i; $x < $lp; $x++) {
        $v = $i+$i;
        $m[$i][$x+1] = $nArr[$x+$v];
    }
}

this is my output :

1 + A + B + C + - 1 + D + E + - 1 +   + - 1 +

The output that i want:

1 + A + B + C + - 1 + D + E + - 1 + F + - 1 +

in detail:

echo $m[0][0]."+"; 1
echo $m[0][1]."+"; A
echo $m[0][2]."+"; B
echo $m[0][3]."+"; C
echo " - ";
echo $m[1][1]."+"; 1
echo $m[1][2]."+"; D
echo $m[1][3]."+"; E
echo "- ";
echo $m[2][2]."+";1
echo $m[2][3]."+"; //error, must be F
echo "- ";
echo $m[3][3]."+"; 1
0

4 Answers 4

2

Your problem is in your progression. In the second "for" your progression increases rapidly:

$v = $i+$i;         
$m[$i][$x+1] = $nArr[$x+$v]; 
  • In the first iteration starts with 0 + 0 + 0 ($ x + $ i + $ i) = 0 = "A"
  • In the second iteration begins with 1 + 1 + 1 ($ x + $ i + $ i) = 3 = "D"
  • and the third iteration begins with 2 + 2 + 2 ($ x + $ i + $ i) = 6 = null, your array has 6 elements so your max index is 5.

You should change the progression in order to get the next progression of indexes: 0, 3, 5.

Try this:

$nArr = array('A', 'B', 'C', 'D', 'E', 'F');
$lp = 4;                
for ($i = 0; $i < $lp; $i++) {
     $m[$i][$i] = 1;            
    for ($x = $i; $x < $lp; $x++) {                
        $v = $i+ceil($i/2);
        $m[$i][$x+1] = $nArr[$x+$v];            
    }        
}
Sign up to request clarification or add additional context in comments.

1 Comment

1+B+C+D+ - 1+D+E+- 1+F+- 1+ << output
1

Lol, had to accept the challenge...

$nArr = array('A', 'B', 'C', 'D', 'E', 'F');

$counter = 3;

while ($counter > 0) {    

    $chunkedValues[$counter][0] = 1;

    for ($j = 0 ; $j < $counter ; $j++) {
        $chunkedValues[$counter][$j + 1] = $nArr[$j];
    }

    $nArr = array_slice($nArr, $counter--);
}

var_dump($chunkedValues);

Comments

0

$lp is 4, I bet it should be 5. Or even better set it to count($nArr) - 1.

PS. What in the world is this loop for?

Edit: Never mind, that didn't solve it.

Comments

0

The problem is that your algorithm is wrong. Your code sets m[i][j] =

  • undefined if j < i
  • 1 if j = i
  • nArr[2 * i + j - 1] if j > i

So m[2][3] = nArr[6], which is undefined.

It's not totally clear to me what you're trying to do, or why, but my best guess is this:

$nArr = array('A', 'B', 'C', 'D', 'E', 'F');

$lp = 4;

$nArr_index = 0;
for ($i = 0; $i < $lp; $i++) {
    $m[$i][$i] = 1;
    for ($j = $i + 1; $j <= $lp; $j++) {
        $m[$i][$j] = $nArr[$nArr_index++];
    }
}

1 Comment

1+A+B+C+ - 1+E+F+- 1++- 1+ <<< output

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.