Nicolas, Jochem, Tul,
I have been going mental, reading the online manual and trying different
things. I can not get this loop to work.
Please understand that I have endeavored to read up on list(), each(),
foreach(), while(). key(), and everything else I can. I've even looked
at online tutorials and I have to PHP books here. I'm asking on list not
because I come here before searching for existing help, it's because I
don't understand the help that I've found.
In fact, I've tried lifting code directly from a book I recently bought
on PHP, called "All In One: PHP, MySQL and Apache".
foreach($elements as $e){
while(list($key, $value) = each($e)){
echo "key = " . $key . "<br />";
echo "value = " . $value . "<br />";
}
}
But it still doesn't work. It says:
Warning: Variable passed to each() is not an array or object in
/home/dave/web_sites/thinkingworks.com/web/Article.class on line 60
I tested my code upstream to make sure my array does have the correct
values in it:
echo "This is the id for elements : " . $elements['id'] . "<br />";
echo "This is the type for elements : " . $elements['type'] . "<br />";
echo "This is the resource for elements : " . $elements['resource'] .
"<br />";
echo "This is the parent for elements : " . $elements['parent'] . "<br />";
This outputs:
This is the id for elements : 2
This is the type for elements : Text
This is the resource for elements : 1
This is the parent for elements : 1
Which is all I really want to do, but since it's a multidimensional
array, I can't get at the next set of values that follows the first.
Part of what I don't get, and part of what led me to the first question,
is that I don't get why a while() loop is so easy with
mysql_fetch_array, and yet to act on a multidemensional array in any
other way seems to be so complicated, with so many ways to skin a cat.
I tried what was suggested here on this list, by Tul, too:
foreach($elements as $e){
echo "e[type] = " . $e['type'] . "<br />";
echo "e[resource] = " . $e['resource'] . "<br />";
}
Which produced really weird results:
e[type] = 2
e[resource] = 2
e[type] = 2
e[resource] = 2
e[type] = T
e[resource] = T
e[type] = T
e[resource] = T
e[type] = 1
e[resource] = 1
e[type] = 1
e[resource] = 1
e[type] = 1
e[resource] = 1
e[type] = 1
e[resource] = 1
I could cycle through the other variants I've tried, but the bottom line
remains the same - it ain't working, the results don't match what I'm
seeing in the references, and I don't understand what's going on.
It would be really great if someone could lead me out of the darkness.
Thank you for your advice.
--
Dave M G
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php