{"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

\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

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

\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

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

\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

With all those slashes added, that’s why the output is often “null” when folks get the data outputted.<\/p>\n

My solution: PHP’s stripslashes() to the rescue.<\/h2>\n

Get rid of the slashes first, then json_decode()<\/strong>:<\/p>\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

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":"\nSOLVED: Wordpress Ajax JSON error: Invalid argument supplied for foreach() - User Experience Design, Strategy, and App Development | Travers+Todd<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/traverstodd.com\/solved-wordpress-ajax-json-error-invalid-argument-supplied-for-foreach\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SOLVED: Wordpress Ajax JSON error: Invalid argument supplied for foreach() - User Experience Design, Strategy, and App Development | Travers+Todd\" \/>\n<meta property=\"og:description\" content=\"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 […]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/traverstodd.com\/solved-wordpress-ajax-json-error-invalid-argument-supplied-for-foreach\/\" \/>\n<meta property=\"og:site_name\" content=\"User Experience Design, Strategy, and App Development | Travers+Todd\" \/>\n<meta property=\"article:published_time\" content=\"2015-08-14T15:19:35+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2015-08-14T16:46:32+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/traverstodd.com\/wp-content\/uploads\/pacman-eating-bad-code.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1400\" \/>\n\t<meta property=\"og:image:height\" content=\"602\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Mike\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Mike\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/traverstodd.com\/solved-wordpress-ajax-json-error-invalid-argument-supplied-for-foreach\/\",\"url\":\"https:\/\/traverstodd.com\/solved-wordpress-ajax-json-error-invalid-argument-supplied-for-foreach\/\",\"name\":\"SOLVED: Wordpress Ajax JSON error: Invalid argument supplied for foreach() - User Experience Design, Strategy, and App Development | Travers+Todd\",\"isPartOf\":{\"@id\":\"https:\/\/traverstodd.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/traverstodd.com\/solved-wordpress-ajax-json-error-invalid-argument-supplied-for-foreach\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/traverstodd.com\/solved-wordpress-ajax-json-error-invalid-argument-supplied-for-foreach\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/traverstodd.com\/wp-content\/uploads\/pacman-eating-bad-code.jpg\",\"datePublished\":\"2015-08-14T15:19:35+00:00\",\"dateModified\":\"2015-08-14T16:46:32+00:00\",\"author\":{\"@id\":\"https:\/\/traverstodd.com\/#\/schema\/person\/d08b7a6f55746db1e358218126f32bf6\"},\"breadcrumb\":{\"@id\":\"https:\/\/traverstodd.com\/solved-wordpress-ajax-json-error-invalid-argument-supplied-for-foreach\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/traverstodd.com\/solved-wordpress-ajax-json-error-invalid-argument-supplied-for-foreach\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/traverstodd.com\/solved-wordpress-ajax-json-error-invalid-argument-supplied-for-foreach\/#primaryimage\",\"url\":\"https:\/\/traverstodd.com\/wp-content\/uploads\/pacman-eating-bad-code.jpg\",\"contentUrl\":\"https:\/\/traverstodd.com\/wp-content\/uploads\/pacman-eating-bad-code.jpg\",\"width\":1400,\"height\":602},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/traverstodd.com\/solved-wordpress-ajax-json-error-invalid-argument-supplied-for-foreach\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/traverstodd.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SOLVED: WordPress Ajax JSON error: Invalid argument supplied for foreach()\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/traverstodd.com\/#website\",\"url\":\"https:\/\/traverstodd.com\/\",\"name\":\"User Experience Design, Strategy, and App Development | Travers+Todd\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/traverstodd.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/traverstodd.com\/#\/schema\/person\/d08b7a6f55746db1e358218126f32bf6\",\"name\":\"Mike\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/traverstodd.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/0993e33b53af46ecde067e7cfd6279b3?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/0993e33b53af46ecde067e7cfd6279b3?s=96&d=mm&r=g\",\"caption\":\"Mike\"},\"url\":\"https:\/\/traverstodd.com\/author\/mike\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"SOLVED: Wordpress Ajax JSON error: Invalid argument supplied for foreach() - User Experience Design, Strategy, and App Development | Travers+Todd","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/traverstodd.com\/solved-wordpress-ajax-json-error-invalid-argument-supplied-for-foreach\/","og_locale":"en_US","og_type":"article","og_title":"SOLVED: Wordpress Ajax JSON error: Invalid argument supplied for foreach() - User Experience Design, Strategy, and App Development | Travers+Todd","og_description":"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 […]","og_url":"https:\/\/traverstodd.com\/solved-wordpress-ajax-json-error-invalid-argument-supplied-for-foreach\/","og_site_name":"User Experience Design, Strategy, and App Development | Travers+Todd","article_published_time":"2015-08-14T15:19:35+00:00","article_modified_time":"2015-08-14T16:46:32+00:00","og_image":[{"width":1400,"height":602,"url":"https:\/\/traverstodd.com\/wp-content\/uploads\/pacman-eating-bad-code.jpg","type":"image\/jpeg"}],"author":"Mike","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Mike","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/traverstodd.com\/solved-wordpress-ajax-json-error-invalid-argument-supplied-for-foreach\/","url":"https:\/\/traverstodd.com\/solved-wordpress-ajax-json-error-invalid-argument-supplied-for-foreach\/","name":"SOLVED: Wordpress Ajax JSON error: Invalid argument supplied for foreach() - User Experience Design, Strategy, and App Development | Travers+Todd","isPartOf":{"@id":"https:\/\/traverstodd.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/traverstodd.com\/solved-wordpress-ajax-json-error-invalid-argument-supplied-for-foreach\/#primaryimage"},"image":{"@id":"https:\/\/traverstodd.com\/solved-wordpress-ajax-json-error-invalid-argument-supplied-for-foreach\/#primaryimage"},"thumbnailUrl":"https:\/\/traverstodd.com\/wp-content\/uploads\/pacman-eating-bad-code.jpg","datePublished":"2015-08-14T15:19:35+00:00","dateModified":"2015-08-14T16:46:32+00:00","author":{"@id":"https:\/\/traverstodd.com\/#\/schema\/person\/d08b7a6f55746db1e358218126f32bf6"},"breadcrumb":{"@id":"https:\/\/traverstodd.com\/solved-wordpress-ajax-json-error-invalid-argument-supplied-for-foreach\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/traverstodd.com\/solved-wordpress-ajax-json-error-invalid-argument-supplied-for-foreach\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/traverstodd.com\/solved-wordpress-ajax-json-error-invalid-argument-supplied-for-foreach\/#primaryimage","url":"https:\/\/traverstodd.com\/wp-content\/uploads\/pacman-eating-bad-code.jpg","contentUrl":"https:\/\/traverstodd.com\/wp-content\/uploads\/pacman-eating-bad-code.jpg","width":1400,"height":602},{"@type":"BreadcrumbList","@id":"https:\/\/traverstodd.com\/solved-wordpress-ajax-json-error-invalid-argument-supplied-for-foreach\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/traverstodd.com\/"},{"@type":"ListItem","position":2,"name":"SOLVED: WordPress Ajax JSON error: Invalid argument supplied for foreach()"}]},{"@type":"WebSite","@id":"https:\/\/traverstodd.com\/#website","url":"https:\/\/traverstodd.com\/","name":"User Experience Design, Strategy, and App Development | Travers+Todd","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/traverstodd.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/traverstodd.com\/#\/schema\/person\/d08b7a6f55746db1e358218126f32bf6","name":"Mike","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/traverstodd.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/0993e33b53af46ecde067e7cfd6279b3?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/0993e33b53af46ecde067e7cfd6279b3?s=96&d=mm&r=g","caption":"Mike"},"url":"https:\/\/traverstodd.com\/author\/mike\/"}]}},"_links":{"self":[{"href":"https:\/\/traverstodd.com\/wp-json\/wp\/v2\/posts\/522"}],"collection":[{"href":"https:\/\/traverstodd.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/traverstodd.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/traverstodd.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/traverstodd.com\/wp-json\/wp\/v2\/comments?post=522"}],"version-history":[{"count":0,"href":"https:\/\/traverstodd.com\/wp-json\/wp\/v2\/posts\/522\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/traverstodd.com\/wp-json\/wp\/v2\/media\/526"}],"wp:attachment":[{"href":"https:\/\/traverstodd.com\/wp-json\/wp\/v2\/media?parent=522"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/traverstodd.com\/wp-json\/wp\/v2\/categories?post=522"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/traverstodd.com\/wp-json\/wp\/v2\/tags?post=522"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}