williamZanelli Posted July 13, 2009 Share Posted July 13, 2009 Hi guys, Just wondering if you could tell me whats wrong with the following array $array2d= array( 5 => array( 'fname' => 'Smith', 'sname' => 'smith' ), ); Its had me all day, when I run it inside a foreach, it throws the following warning, Warning: Invalid argument supplied for foreach() - followed by the line number. Thanks for your thoughts Will Quote Link to comment https://forums.phpfreaks.com/topic/165803-foreach-invalid-argument-supplied/ Share on other sites More sharing options...
Maq Posted July 13, 2009 Share Posted July 13, 2009 Can we see all the relevant code? If you put that in a foreach it will work. Quote Link to comment https://forums.phpfreaks.com/topic/165803-foreach-invalid-argument-supplied/#findComment-874549 Share on other sites More sharing options...
ignace Posted July 13, 2009 Share Posted July 13, 2009 With the array is not wrong, with your code proably is. Quote Link to comment https://forums.phpfreaks.com/topic/165803-foreach-invalid-argument-supplied/#findComment-874551 Share on other sites More sharing options...
williamZanelli Posted July 13, 2009 Author Share Posted July 13, 2009 Hi guys, Thanks for the prompt responses. The code I have is cumbersome, so I decided to keep it simple. The foreach is like, foreach($array2d as $key => $val) { if($data[$key] != '') { $sql = "......" //loads of business logic etc. The error is thrown at line foreach. Thanks for your thoughts Will Quote Link to comment https://forums.phpfreaks.com/topic/165803-foreach-invalid-argument-supplied/#findComment-874566 Share on other sites More sharing options...
J.Daniels Posted July 13, 2009 Share Posted July 13, 2009 You should var_dump the array variable to make sure it is correctly populated. Quote Link to comment https://forums.phpfreaks.com/topic/165803-foreach-invalid-argument-supplied/#findComment-874569 Share on other sites More sharing options...
.josh Posted July 13, 2009 Share Posted July 13, 2009 do you do something with $array2d sometime between where you assign it and run that foreach? Invalid argument means it is not an array. .. Quote Link to comment https://forums.phpfreaks.com/topic/165803-foreach-invalid-argument-supplied/#findComment-874570 Share on other sites More sharing options...
williamZanelli Posted July 13, 2009 Author Share Posted July 13, 2009 Thanks for this guys. How do I do a var dump? Cheers Quote Link to comment https://forums.phpfreaks.com/topic/165803-foreach-invalid-argument-supplied/#findComment-874578 Share on other sites More sharing options...
Maq Posted July 13, 2009 Share Posted July 13, 2009 Thanks for this guys. How do I do a var dump? Cheers You can do: print_r($array2d); //or var_dump($array2d); Like CV said, you must be changing your array between the codes you've posted. Please post all the code from your first except to the second. Quote Link to comment https://forums.phpfreaks.com/topic/165803-foreach-invalid-argument-supplied/#findComment-874588 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.