0

After some time, I get the following warning:

Notice: Undefined offset: 3 in (...) on line 8

<?php

$arrnumbers = array(1=>array(10,20,30),2=>array(40,50,60));

for($indexRow =1; $indexRow <=count($arrnumbers); ++$indexRow)
{
    for($indexColumn =0; $indexCoulumn <count($arrnumbers[$indexRow]);++$indexColumn)
    {
    printf("%d ", $arrnumbers[$indexRow][$indexColumn]);
    }

print("<BR>");
}
?>

thanks in advance!

noname
  • 3
  • 1
  • You probably want to use a `foreach` instead of `for`-loops. Much cleaner when dealing with arrays. – Qirel Oct 24 '15 at 11:08
  • You also have a typo in your code, which is why it doesn't work. You have `$indexCoulumn` instead of `$indexColumn` in the second `for`-loop. – Qirel Oct 24 '15 at 11:27
  • I'd still recommend using a `foreach`-loop, it's a lot simpler! Take a look at my answer below. :) – Qirel Oct 24 '15 at 11:35

1 Answers1

0

As I commented on your question, I'd suggest using a foreach-loop for dealing with arrays, it's much cleaner and simpler.

The code below first loops through the first sub-array and prints it, then does a newline and does the same thing for the second sub-array. The output will be

10 20 30
40 50 60

<?php
$arrnumbers = array(1=>array(10,20,30),2=>array(40,50,60));

foreach ($arrnumbers as $val) {
    foreach ($val as $key=>$value) {
        printf("%d ", $value);
    }

    print("<br />");
}
?>

You can read the full documentation on foreach-loops at PHP.net.

Qirel
  • 25,449
  • 7
  • 45
  • 62