Invalid Argument Supplied For Foreach Php Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about invalid argument supplied for foreach() wordpress Stack Overflow the company Business Learn more about hiring developers or posting ads
How To Solve Warning Invalid Argument Supplied For Foreach In Php
with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow invalid argument supplied for foreach() laravel is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Invalid argument supplied for foreach() up vote 157 down vote favorite 46 It
Invalid Argument Supplied For Foreach() Codeigniter
often happens to me to handle data that can be either an array or a null variable and to feed some foreach with these data. $values = get_values(); foreach ($values as $value){ ... } When you feed a foreach with data that are not an array, you get a warning: Warning: Invalid argument supplied for foreach() in [...] Assuming it's not possible to refactor the get_values() function to always return invalid argument supplied for foreach() laravel 5 an array (backward compatibility, not available source code, whatever other reason), I'm wondering which is the cleanest and most efficient way to avoid these warnings: Casting $values to array Initializing $values to array Wrapping the foreach with an if Other (please suggest) php foreach share|improve this question edited Sep 21 at 16:52 Geoffrey Hale 2,13511723 asked Apr 13 '10 at 13:48 Roberto Aloi 17.3k125190 add a comment| 15 Answers 15 active oldest votes up vote 267 down vote accepted Personally I find this to be the most clean - not sure if it's the most efficient, mind! if (is_array($values) || is_object($values)) { foreach ($values as $value) { ... } } The reason for my preference is it doesn't allocate an empty array when you've got nothing to begin with anyway. share|improve this answer edited Oct 27 '15 at 20:57 vlasits 1,218723 answered Apr 13 '10 at 13:51 Andy Shellam 10.4k11734 2 Or use count() to figure out if array isn't empty –Kemo Apr 13 '10 at 13:59 47 @Kemo: count() is not reliable. If you pass count() null, it returns 0. If you pass it a non-null, non-array argument, it returns 1. Therefore it's impossible to use count() to determine if the variable is
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers
Warning: Invalid Argument Supplied For Foreach() In Joomla
or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x
Invalid Argument Supplied For Foreach() Yii
Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it invalid argument supplied for foreach() array only takes a minute: Sign up Invalid argument supplied for foreach() up vote 2 down vote favorite 1 How can fix error Message: Invalid argument supplied for foreach() - Line Number: 28 in following foreach ? http://stackoverflow.com/questions/2630013/invalid-argument-supplied-for-foreach $mileage_input = $this->input->post('mileage'); foreach ($mileage_input as $idx => $name) { //Line 28 $mileage[] = array( 'mileage' => $mileage_input[$idx] ); } $data = array( 'mileage' => json_encode($mileage), 'customer_number' => $customer_number, 'name' => $this->input->post('name') ); $this->db->insert('customer', $data); ?> php arrays codeigniter foreach share|improve this question asked Sep 20 '11 at 19:20 Jennifer Anthony 83362340 Try var_dump($mileage_input) and see what you get. If you don't get an Array then it won't work. –Niet the Dark Absol Sep 20 '11 at 19:22 http://stackoverflow.com/questions/7490577/invalid-argument-supplied-for-foreach 3 The mileage' field must be named mileage[]` in the html form, e.g. , otherwise PHP will NOT auto-convert it to an array in the _POST/_GET arrays. –Marc B Sep 20 '11 at 19:25 possible duplicate of Invalid argument supplied for foreach() –Félix Gagnon-Grenier Jul 7 '15 at 18:59 add a comment| 2 Answers 2 active oldest votes up vote 5 down vote accepted $mileage_input is probably not an array, which is why it isn't working. share|improve this answer answered Sep 20 '11 at 19:24 Chris G. 2,94721031 add a comment| up vote 0 down vote Most likely $mileage_input is not an array. Perhaps you must error check $this->input->post. If you dont really care about the error, but simply want to not get the error you can cast the value to an array before looping over it. foreach((array)$mileage_input as $idx => $name { share|improve this answer answered Sep 20 '11 at 19:26 Alexander Olsson 774718 2 Hiding the dust under the carpet won't make your house cleaner. –Matteo Riva Sep 20 '11 at 19:57 Very often you can benefit from having e.g. NULL passed to your function, in which case you do not want to do what the loop normally does. Casting NULL to an array gives an empty array and consequently no loops in the foreach will be run. I've found it very useful from time to time. –A
for foreach() errors [Resolved] Invalid argument supplied for foreach() errors Peter Hardy-vanDoorn @petervandoorn 10 months, 3 weeks ago Since updating to the latest version I'm seeing this error, but only on the backend: Warning: Invalid argument supplied https://wordpress.org/support/topic/invalid-argument-supplied-for-foreach-errors/ for foreach() in /.../wp-content/plugins/advanced-access-manager/Application/Backend/Extension.php on line 87 and also on line 169 when viewing /admin.php?page=aam https://wordpress.org/plugins/advanced-access-manager/ Viewing 9 replies - 1 through 9 (of 9 total) Plugin Author Vasyltech @vasyl_m 10 months, 3 weeks ago Hi Peter, Thank you for your feedback. That is kind of a weird warning, considering the fact that on the line 87 there is no foreach loop. Please try invalid argument to install AAM Utilities extension and clear all settings. This might help you to avoid the second warning. Keep us updated. Regards, Vasyl Peter Hardy-vanDoorn @petervandoorn 10 months, 3 weeks ago I had thought to try the Utilities extension, but unfortunately the 2 errors completely break all AAM functionality, so the Download button doesn't work! I have also already tried deleting the plugin files and invalid argument supplied reinstalling via FTP, but I don't really want to try deleting it through WP as I don't really want to delete the extensive user role settings! Plugin Author Vasyltech @vasyl_m 10 months, 3 weeks ago Hi Peter, As a quick fix, you might try to go to database and remove the option aam-extension-list in your wp_options table. The permanent fix is coming with our new release AAM 3.0.4 in couple days. Thank you for your feedback and let me know if this works for you. Regards, Vasyl Studiobovenkamer @studiobovenkamer 8 months, 3 weeks ago Hi Vasyl, Was this problem resolved? I'm running 3.0.9 and get the same warning: Warning: Invalid argument supplied for foreach() in ../wp-content/plugins/advanced-access-manager/Application/Backend/Manager.php on line 87. Warning: Invalid argument supplied for foreach() in ../wp-content/plugins/advanced-access-manager/Application/Backend/Extension.php on line 180 When visiting the AAM page in the admin I also get three alert pop-ups, all with the same warning: DataTables warning: table id=role-list - Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1 Opening the extension tab, there one more warning: Warning: Invalid argument supplied for foreach() in ../wp-content/plugins/advanced-access-manager/Application/Core/Repository.php on line 224 Plugin Author Vasyltech @vasyl_m 8 months, 3