tlavelle Posted August 13, 2007 Share Posted August 13, 2007 Warning: Invalid argument supplied for foreach() in /home/scrynet/public_html/do How do I re-engineer this code to get rid of the above warning? function template ($file, $array) { $handle = fopen ('theme/' . gen ('theme') . ('' . '/templates/' . $file), 'r'); $template = fread ($handle, 1024 * 1024); fclose ($handle); foreach ($array as $code => $value) { $template = str_replace ('{{' . $code . '}}', $value, $template); } return $template; } Quote Link to comment https://forums.phpfreaks.com/topic/64696-warning-invalid-argument-supplied-for-foreach-in-homescrynetpublic_htmldo/ Share on other sites More sharing options...
NArc0t1c Posted August 13, 2007 Share Posted August 13, 2007 Check if the array variable isn't empty. Quote Link to comment https://forums.phpfreaks.com/topic/64696-warning-invalid-argument-supplied-for-foreach-in-homescrynetpublic_htmldo/#findComment-322596 Share on other sites More sharing options...
trq Posted August 13, 2007 Share Posted August 13, 2007 The error simply means that $array is not an array. Quote Link to comment https://forums.phpfreaks.com/topic/64696-warning-invalid-argument-supplied-for-foreach-in-homescrynetpublic_htmldo/#findComment-322598 Share on other sites More sharing options...
akitchin Posted August 13, 2007 Share Posted August 13, 2007 you can optionally use is_array(). this will avoid issues if you pass it a string, in which case the variable will not be empty but will still trigger the foreach() error. Quote Link to comment https://forums.phpfreaks.com/topic/64696-warning-invalid-argument-supplied-for-foreach-in-homescrynetpublic_htmldo/#findComment-322600 Share on other sites More sharing options...
tlavelle Posted August 14, 2007 Author Share Posted August 14, 2007 Not sure how to use is_array with this function. Don't I need to check to see if it is array before it gets to the function? I attempted to implement it and got a blank page function template ($file, $array) { $handle = fopen ('theme/' . gen ('theme') . ('' . '/templates/' . $file), 'r'); $template = fread ($handle, 1024 * 1024); fclose ($handle); foreach ($array as $code => $value) { $template = str_replace ('{{' . $code . '}}', $value, $template); } return $template; } Quote Link to comment https://forums.phpfreaks.com/topic/64696-warning-invalid-argument-supplied-for-foreach-in-homescrynetpublic_htmldo/#findComment-323669 Share on other sites More sharing options...
akitchin Posted August 14, 2007 Share Posted August 14, 2007 function template ($file, $array) { $handle = fopen ('theme/' . gen ('theme') . ('' . '/templates/' . $file), 'r'); $template = fread ($handle, 1024 * 1024); fclose ($handle); if (is_array($array)) { foreach ($array as $code => $value) { $template = str_replace ('{{' . $code . '}}', $value, $template); } } return $template; } this will only stop the error from arising. if you REQUIRE $array to be an array, you're right, you'll need to check before it's sent (or define a default array). Quote Link to comment https://forums.phpfreaks.com/topic/64696-warning-invalid-argument-supplied-for-foreach-in-homescrynetpublic_htmldo/#findComment-323680 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.