Support » Developing with WordPress » 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

    (@catacaustic)

    very awesome

    How are you actually trying to save the values, and what would the array actually look like?

    jspellman

    (@jspellman)

    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

    (@bcworkz)

    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.

    jspellman

    (@jspellman)

    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)
  • You must be logged in to reply to this topic.