Title: Multidimensional array in widget
Last modified: May 17, 2017

---

# Multidimensional array in widget

 *  [John Spellman](https://wordpress.org/support/users/jspellman/)
 * (@jspellman)
 * [9 years ago](https://wordpress.org/support/topic/multidimensional-array-in-widget/)
 * Hi there,
 * I am wondering if it is possible to create, save to, and use data from a multidimensional
   array in WP_Widget::form. My idea is to have multiple text fields accompanied
   with a weight (number) field. The order of the text field output will be determined
   using the numbers in the weight field.
 * Was hoping something along these lines would work:
 *     ```
       $i = 0;
                 foreach ($field as $name):
                   $i++;
                   $name = ltrim($name, '_'); ?>
                     <li>
                         <label>
                           <?php echo $name; ?>
                             <input type="checkbox"
                                    id="<?php echo $name; ?>"
                                    value="<?php echo $name; ?>"
                                    name="<?php echo $this->get_field_name('fields'); ?>[]" <?php checked(is_array($instance['fields'][$i]['field']) && in_array($name, $instance['fields'][$i]['field'])); ?> />
                             <input class="widefat"
                                    id="<?php echo $name; ?>-weight"
                                    name="<?php echo $this->get_field_name('fields'); ?>[]"
                                    type="text"
                                    value="<?php echo sanitize_html_class($instance['fields'][$i]['weight']); ?>"/>
                         </label>
                     </li>
                   <?php
                 endforeach; ?>
       ```
   
 * but I haven’t had any luck with saving/output. Let me know if this is something
   that’s possible, or if more detail is needed. Thanks!

Viewing 4 replies - 1 through 4 (of 4 total)

 *  [catacaustic](https://wordpress.org/support/users/catacaustic/)
 * (@catacaustic)
 * [9 years ago](https://wordpress.org/support/topic/multidimensional-array-in-widget/#post-9143255)
 * How are you actually trying to save the values, and what would the array actually
   look like?
 *  Thread Starter [John Spellman](https://wordpress.org/support/users/jspellman/)
 * (@jspellman)
 * [9 years ago](https://wordpress.org/support/topic/multidimensional-array-in-widget/#post-9145800)
 * That’s part of my problem, I’m not sure how to save a multidimensional array 
   and have been unable to find any documentation/examples on how to do so.
 * I’d like to save two values: a name and a number. Right now I am using $instance[‘
   fields’] = $new_instance[‘fields’] which is obviously not working.
 * Ideally the array would look like this:
 *     ```
       array (size=2)
         0 => 
           array (size=2)
             0 => string '-9'
             1 => string 'field_1'
         1 => 
           array (size=2)
             0 => string '-10'
             1 => string 'field_2'
       ```
   
 * I’ve come up with a workaround, but this solution seems to be more ideal.
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [9 years ago](https://wordpress.org/support/topic/multidimensional-array-in-widget/#post-9146427)
 * I’m sorry, but I don’t see the point. The Widget API already stores all form 
   settings in an array, though not in the form you would like. I suggest you let
   the API handle data the way it wants to. In your widget front end method, you
   could construct an array in the form you prefer from the API data if that would
   somehow benefit how the output is managed. I’d simply work with the data as handled
   by the API if at all possible.
 * To answer your question anyway, you should be able to add a new element to the
   instance array to contain your array within your update() method, but this would
   be creating redundant data because the API still needs to manage the same data
   the way it wants to. Redundant data really needs to be avoided if at all possible.
 *  Thread Starter [John Spellman](https://wordpress.org/support/users/jspellman/)
 * (@jspellman)
 * [9 years ago](https://wordpress.org/support/topic/multidimensional-array-in-widget/#post-9146940)
 * Yep, that’s what I figured. Just wanted to be sure I wasn’t missing anything.
 * Thanks for the feedback bcworkz and catacaustic!

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Multidimensional array in widget’ is closed to new replies.

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 4 replies
 * 3 participants
 * Last reply from: [John Spellman](https://wordpress.org/support/users/jspellman/)
 * Last activity: [9 years ago](https://wordpress.org/support/topic/multidimensional-array-in-widget/#post-9146940)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
