statement foreach ... help!!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
valentik
Forum Newbie
Posts: 2
Joined: Mon Apr 07, 2003 3:11 am
Contact:

statement foreach ... help!!

Post 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
User avatar
d1223m
Forum Commoner
Posts: 80
Joined: Mon Mar 31, 2003 5:15 am
Location: UK, West Sussex

Post by d1223m »

oh i see its actually an example from the manual :?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
d1223m
Forum Commoner
Posts: 80
Joined: Mon Mar 31, 2003 5:15 am
Location: UK, West Sussex

Post by d1223m »

im wondering if you have an old version of php maybe?
User avatar
d1223m
Forum Commoner
Posts: 80
Joined: Mon Mar 31, 2003 5:15 am
Location: UK, West Sussex

Post by d1223m »

ok - its not your version
Post Reply