{"id":522,"date":"2015-08-14T08:19:35","date_gmt":"2015-08-14T15:19:35","guid":{"rendered":"https:\/\/traverstodd.com\/?p=522"},"modified":"2015-08-14T09:46:32","modified_gmt":"2015-08-14T16:46:32","slug":"solved-wordpress-ajax-json-error-invalid-argument-supplied-for-foreach","status":"publish","type":"post","link":"https:\/\/traverstodd.com\/solved-wordpress-ajax-json-error-invalid-argument-supplied-for-foreach\/","title":{"rendered":"SOLVED: WordPress Ajax JSON error: Invalid argument supplied for foreach()"},"content":{"rendered":"
So, you’re doing some WordPress AJAX. And maybe you’re serializing your form data into an array of objects. JSON.stringify()<\/strong> and fire that into wp-ajax.php<\/strong>. And maybe you console.log<\/strong>‘d it to verify it’s an array of objects… Cool.<\/p>\n Maybe it looked like this (i.e. an array of objects):<\/p>\n But then in functions.php<\/strong>, you json_decode()<\/strong> your lovely $_POST[]<\/strong> data only to find PHP warning you that it’s not an array? “Invalid argument supplied foreach()<\/strong>“?? Heads will roll!!<\/p>\n Google gave me a bunch of StackOverflow and WordPress support articles, which all suggested the wrong thing (for me, at least). They point to adding “true” as a parameter into json_decode()<\/strong>, which only turns your array of objects into an array of associative arrays. Or they tell you to use PHP’s is_array()<\/strong> or is_object()<\/strong> conditional checks on the $_POST[]<\/strong> data.<\/p>\n None of it worked. PHP just didn’t know it was an array. Some articles suggested PHP’s nearly-deprecated magic-quotes was on. Nope. It was off. Finally after getting the $_POST[]<\/strong> data and taking a look, I could see the culprit (a bunch of added slashes):<\/p>\n With all those slashes added, that’s why the output is often “null” when folks get the data outputted.<\/p>\n Get rid of the slashes first, then json_decode()<\/strong>:<\/p>\n Now you’re dancin’. Hope this helps your project. Let me know if it did!<\/p>\n","protected":false},"excerpt":{"rendered":" So, you’re doing some WordPress AJAX. And maybe you’re serializing your form data into an array of objects. JSON.stringify() and fire that into wp-ajax.php. And maybe you console.log‘d it to verify it’s an array of objects… Cool. Maybe it looked like this (i.e. an array of objects): [{“name”:”event_name”,”value”:”My cool Title”},{“name”:”event_start_date”,”value”:”2015\/08\/06″},{“name”:”event_start_time”,”value”:”12:30:AM”},{“name”:”event_end_date”,”value”:”2015\/08\/06″},{“name”:”event_end_time”,”value”:”01:00:AM”}] But then in functions.php, you […]<\/p>\n","protected":false},"author":1,"featured_media":526,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"acf":[],"yoast_head":"\n\r\n[{\"name\":\"event_name\",\"value\":\"My cool Title\"},{\"name\":\"event_start_date\",\"value\":\"2015\/08\/06\"},{\"name\":\"event_start_time\",\"value\":\"12:30:AM\"},{\"name\":\"event_end_date\",\"value\":\"2015\/08\/06\"},{\"name\":\"event_end_time\",\"value\":\"01:00:AM\"}]\r\n<\/code><\/pre>\n
\r\n$form_data = json_decode( $_POST['formData'] );\r\n\/\/ my awesome array of objects, verified through the console beforeSend...\r\nforeach($form_data as $form_data_point) {\r\n\/\/ throws the damn warning: Invalid argument supplied foreach() !!!\r\n}\r\n<\/code><\/pre>\n
Yeah, I got that too.<\/h2>\n
\r\n[{\\\"name\\\":\\\"event_name\\\",\\\"value\\\":\\\"My cool Title\\\"},{\\\"name\\\":\\\"event_start_date\\\",\\\"value\\\":\\\"2015\/08\/06\\\"},{\\\"name\\\":\\\"event_start_time\\\",\\\"value\\\":\\\"12:30:AM\\\"},{\\\"name\\\":\\\"event_end_date\\\",\\\"value\\\":\\\"2015\/08\/06\\\"},{\\\"name\\\":\\\"event_end_time\\\",\\\"value\\\":\\\"01:00:AM\\\"}]\r\n<\/code><\/pre>\n
My solution: PHP’s stripslashes() to the rescue.<\/h2>\n
\r\n$form_data = json_decode( stripslashes($_POST['formData']) );\r\n\/\/ look above. we strip the slashes before json decoding\r\nforeach($form_data as $form_data_point) {\r\n\/\/ now we can finally do something with this friggin data...\r\n}<\/code><\/pre>\n