Northern Flame Posted December 16, 2007 Share Posted December 16, 2007 I have a script that I am working on and I had a problem the the foreach() command. I was having a hard time figuring out why it wasnt working and I was messing around with some stuff and I figured out that I cant use the array that I established in my function. Take the following sample script as an example: <?php function testIng(){ $char['test'] = 'test'; $char['testing'] = 'testing'; global $char; } testIng(); foreach($char as $key => $val){ echo $key . ' - ' . $val . "<br>\n"; } ?> with that I get this error Warning: Invalid argument supplied for foreach() in /path/to/my/website.com/script.php on line 8 can anyone tell my why I cant use the array established in the function testIng()? Link to comment https://forums.phpfreaks.com/topic/81886-solved-getting-array-from-a-function/ Share on other sites More sharing options...
Trium918 Posted December 16, 2007 Share Posted December 16, 2007 <?php /* Try this! Result test - test testing - testing */ function testIng(){ $char['test'] = 'test'; $char['testing'] = 'testing'; return $char; } $char = testIng(); foreach($char as $key => $val){ echo $key . ' - ' . $val . "<br>\n"; } ?> Link to comment https://forums.phpfreaks.com/topic/81886-solved-getting-array-from-a-function/#findComment-416051 Share on other sites More sharing options...
Northern Flame Posted December 16, 2007 Author Share Posted December 16, 2007 Thanks a lot! WORKS PERFECTLY! Link to comment https://forums.phpfreaks.com/topic/81886-solved-getting-array-from-a-function/#findComment-416054 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.