{"id":2814282,"date":"2012-06-14T08:07:00","date_gmt":"2012-06-14T08:07:00","guid":{"rendered":"https:\/\/wordpress.org\/support\/topic\/how-to-save-array-values-in-a-widget\/"},"modified":"2016-08-20T12:55:43","modified_gmt":"2016-08-20T12:55:43","slug":"how-to-save-array-values-in-a-widget","status":"closed","type":"topic","link":"https:\/\/wordpress.org\/support\/topic\/how-to-save-array-values-in-a-widget\/","title":{"rendered":"How to save array values in a widget"},"content":{"rendered":"<p>Ladies and gents,<\/p>\n<p>I&#8217;m working on a widget and in that widget I want a few &lt;select&gt; fields and a few URL fields. In the example I&#8217;m going to give, you see the URL fields. I want to put those fields in an array and place that in the DB and read that same array out again.<\/p>\n<p>I&#8217;m not sure what I&#8217;m doing wrong, but all the fields get the value &#8216;array&#8217; now.<\/p>\n<pre><code>&lt;?php\nfunction form($instance) {\n        $url = esc_attr($instance[&#039;url&#039;]);\n        for($i=0;$i&lt;5;$i++) {\n        ?&gt;\n        &lt;p&gt;\n            &lt;?php print_r($url); ?&gt;\n\t\t\t&lt;label for=&quot;&lt;?php echo $this-&gt;get_field_id(&#039;url&#039;); ?&gt;&quot;&gt;&lt;?php _e(&#039;URL:&#039;); ?&gt;\n\t\t\t\t&lt;input class=&quot;widefat&quot; id=&quot;&lt;?php echo $this-&gt;get_field_id(&#039;url&#039;); ?&gt;-&lt;?php echo $i; ?&gt;&quot; name=&quot;&lt;?php echo $this-&gt;get_field_name(&#039;url&#039;); ?&gt;[]&quot; type=&quot;text&quot; value=&quot;&lt;?php echo $url; ?&gt;&quot; \/&gt;\n\t\t\t&lt;\/label&gt;\n\t\t&lt;\/p&gt;\n\t&lt;?php\n        }\n        ?&gt;<\/code><\/pre>\n<pre><code>&lt;?php\nfunction update($new_instance,$old_instance) {\n\t\t$instance = $old_instance;\n\n        for($i=0;$i&lt;5;$i++) {\n            $instance[&#039;url&#039;] = esc_attr($new_instance[&#039;url&#039;]);\n        }\n\n\t\treturn $instance;\n\t}\n?&gt;<\/code><\/pre>\n<p>I hope some one can give me a shove in the right direction.<\/p>\n<p>Thanks<\/p>\n","protected":false},"template":"","class_list":["post-2814282","topic","type-topic","status-closed","hentry","topic-tag-array","topic-tag-widget"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/2814282","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic"}],"about":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/types\/topic"}],"version-history":[{"count":0,"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/2814282\/revisions"}],"wp:attachment":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/media?parent=2814282"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}