Re: question about foreach and associate array

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, February 20, 2006 8:57 pm, jonathan wrote:
> I have the following construct:
>
>       $arg['textarea']['body']="Hello";
>
>
>        foreach($arg['textarea'] as $row)
>        {
> 	   echo $row['body']."<br/>";
> 	   echo $arg['textarea']['body']."<br/>";
>
>        }
>
> I would expect both of them to output "Hello" but only the second
> does. The first outputs "H". I thought I have done this before. Can
> anybody tell me why this won't work?

It's like this:

foreach($arg['textarea'] as $row) is ALREADY tearing apart the
$arg['textarea'] into pieces.

So $row is "Hello"

When you print $row['body'], PHP is kinda stuck...

You've got a string, which is really a bunch of individual characters
run together.

You're trying to index it like an array.

So you MUST be trying to get at an individual character.

The index you've given is 'body', and PHP needs that to be a number:
0->H
1->e
2->l
3->l
4->o

The data-type conversion rules of PHP convert 'body' to the number 0.

So you're getting 'H' because you are really doing:
$row = "Hello";
echo $row[0];

Maybe what you REALLY wanted was:
//iterate through each 'textarea':
foreach($arg as $row){
  echo $row['body'];
}

Or, perhaps:
foreach($arg['textarea'] as $row){
  echo $row;
}

-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux