Page 1 of 1

statement foreach ... help!!

Posted: Mon Apr 07, 2003 4:22 am
by valentik
Excuse for my English, I am Italian and I try to learn PHP.
You would know me what does not go in this

$a[0][0] = "a";
$a[0][1] = "b";
$a[1][0] = "y";
$a[1][1] = "z";

foreach($a as $v1) {
foreach ($v1 as $v2) { // line 42
print "$v2\n";
}
}

Warning: Invalid argument supplied for foreach() in /usr/local/apache2/htdocs/test/foreach.php on line 42

Posted: Mon Apr 07, 2003 4:30 am
by d1223m
oh i see its actually an example from the manual :?

Posted: Mon Apr 07, 2003 4:33 am
by twigletmac
That's strange, I ran your code and it works fine for me. Perhaps something else in your code is not working as expected, for debugging try:

Code: Select all

$a[0][0] = "a";
$a[0][1] = "b";
$a[1][0] = "y";
$a[1][1] = "z";
echo '<pre>$a contains: ';
print_r($a);
echo '</pre>';
foreach($a as $v1) {
	echo '<pre>$v1 contains: ';
	print_r($v1);
	echo '</pre>';
    foreach ($v1 as $v2) {  // line 42
        print "$v2\n";
    }
}
You should get output that looks like:

Code: Select all

$a contains: Array
(
    &#1111;0] =&gt; Array
        (
            &#1111;0] =&gt; a
            &#1111;1] =&gt; b
        )

    &#1111;1] =&gt; Array
        (
            &#1111;0] =&gt; y
            &#1111;1] =&gt; z
        )

)

$v1 contains: Array
(
    &#1111;0] =&gt; a
    &#1111;1] =&gt; b
)

a b 
$v1 contains: Array
(
    &#1111;0] =&gt; y
    &#1111;1] =&gt; z
)

y z
Mac

Posted: Mon Apr 07, 2003 4:35 am
by twigletmac
d1223m wrote:oh i see its actually an example from the manual :?
valentik - are you using the code 'as is' or did you modify it? If so could we see any modifications you made to it.

Mac

Posted: Mon Apr 07, 2003 4:52 am
by d1223m
im wondering if you have an old version of php maybe?

Posted: Mon Apr 07, 2003 5:23 am
by d1223m
ok - its not your version