Title: update_post_meta array problem
Last modified: August 20, 2016

---

# update_post_meta array problem

 *  [r0bbiem](https://wordpress.org/support/users/r0bbiem/)
 * (@r0bbiem)
 * [13 years, 2 months ago](https://wordpress.org/support/topic/update_post_meta-array-problem/)
 * Hi there, I’m working on a voting system and having trouble with loading/updating/
   saving an array using get_post_meta and update_post_meta.
 * Here’s the jist of it
 *     ```
       $ip = $_SERVER['REMOTE_ADDR'];
       $id = $_POST['post_id'];
       $voter_ips = get_post_meta($id, 'voter_ips');
       array_push($voter_ips, $ip);
       update_post_meta($id, 'voter_ips', $voter_ips);
       ```
   
 * When I do this the first entry is saved correctly and from then on I get an unwanted
   multi-dimensional kind of value such as this;
 * Array ( [0] => Array ( [0] => 123.2.29.197 ) ) Array ( [0] => Array ( [0] => 
   123.2.29.197 ) [1] => 123.2.29.197 )
 * I’ve tried dozens of ways of fixing this but I’m stumped. Do I need to be doing
   some serializing and unserializing here, I had a go of that but with no success
   so far.
 * Any help greatly appreciated as always

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

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [13 years, 2 months ago](https://wordpress.org/support/topic/update_post_meta-array-problem/#post-3556836)
 * Yeah, there’s something odd on how arrays are stored as metadata. This results
   in the odd structure you see when using array_push(). I’m not sure of the solution
   because I don’t fully understand what’s happening, but I think it lies in the
   direction of either `$voter_ips[] = $ip;` or `array_push($voter_ips[0], $ip);`
 * Or maybe the first pass is the problem? Is there a `$voter_ips = array();` before
   all this starts? Somehow, an extra dimension is being introduced and you need
   to counteract that. It may take writing a var_dump() from various points to a
   file. Then analysis of how and where the array dimensions are growing will tell
   you what you need to do.
 * Sorry for the rambling, that’s what I do when I don’t have a real answer 🙂
 *  Thread Starter [r0bbiem](https://wordpress.org/support/users/r0bbiem/)
 * (@r0bbiem)
 * [13 years, 2 months ago](https://wordpress.org/support/topic/update_post_meta-array-problem/#post-3556869)
 * Thanks bcworkz
 * I tried some of your suggestions but in the end I’m getting around it like this
 *     ```
       $ip = $_SERVER['REMOTE_ADDR'];
       $id = $_POST['post_id'];
       $voter_ips = get_post_meta($id, 'voter_ips', true);
       $voter_ips_array = explode(",", $voter_ips);
   
       //use $voter_ips_array for comparison
   
       if(strlen($voter_ips) == 0)
       {
       	$voter_ips .= $ip;
       }
       else
       {
       	$voter_ips .= ',' . $ip;
       }
       update_post_meta($id, 'voter_ips', $voter_ips);
       ```
   
 * Not ideal because I’m also storing a date along with my vote which I’d like to
   cross reference so saving and retrieving a multi-dimensional array would seem
   to be the most elegant solution but for now this will have to do.
    I’m planning
   on storing the ips and dates as alternate values and then using some kind of 
   +1 or -1 system to access and compare the two.
 * If anyone has any more insight on this I’d love to be enlightened.
 *  [catacaustic](https://wordpress.org/support/users/catacaustic/)
 * (@catacaustic)
 * [13 years, 2 months ago](https://wordpress.org/support/topic/update_post_meta-array-problem/#post-3556876)
 * If you’re going to do this properly don’t use the posts meta data to save the
   values. That will work up until a certain point, but when you get a lot of votes
   it’s going to cause bottlenecks at best and crashes at worst.
 * The ideal solution to this is to create a new table in your database that stores
   votes that are referenced to each post and hods the data of IP address, date/
   time, etc. You can add to this with whatever otehr columns you need, like ranking
   or something for a +/-1 situation. This lets you easily look up IP addresses,
   total votes, total scores and more all through the database queries which is 
   a whole lot faster and more efficient then trying to process multi-dimensional
   arrays that could easily be massive.
 *  Thread Starter [r0bbiem](https://wordpress.org/support/users/r0bbiem/)
 * (@r0bbiem)
 * [13 years, 2 months ago](https://wordpress.org/support/topic/update_post_meta-array-problem/#post-3556896)
 * Thanks catacaustic, I’m looking into that approach now.
 * Couple of questions…for something like this is it ok to do all my mysql interactions
   using the $wpdb class, or am I better off using vanilla php and mysql?
 * and
 * I have some $wpdb code for creating a table, but it strikes me that this might
   be something that only has to be done once ever. That leaves me wondering where
   to put the code…putting it in my plugin doesn’t seem to make sense. Am I wrong?
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [13 years, 2 months ago](https://wordpress.org/support/topic/update_post_meta-array-problem/#post-3556906)
 * Whether to use $wpdb for non-WP tables or not might be somewhat debatable, but
   I think $wpdb is a good approach as long as your tables are in the same database
   as WP.
 * The code for creating a table by plugin would called from the plugin’s [activation hook](http://codex.wordpress.org/Plugin_API#Activation.2FDeactivation.2FUninstall).
   You still need to wrap it in a conditional that checks if the table exists since
   people deactivate & reactivate plugins for various reasons.
 *  [catacaustic](https://wordpress.org/support/users/catacaustic/)
 * (@catacaustic)
 * [13 years, 2 months ago](https://wordpress.org/support/topic/update_post_meta-array-problem/#post-3556913)
 * You should use $wpdb. That’s the tried-and-tested way of doing things. As for
   creating the table as a once-off, there’s some graet information at [http://codex.wordpress.org/Creating_Tables_with_Plugins](http://codex.wordpress.org/Creating_Tables_with_Plugins)
   that will answer that and a whole lot more. Having the code for creating the 
   database table/s in your plugin depends on what you’re going ot do with it. If
   you’re going to only use that plugin for yourself then it’s not a big deal, but
   if you’re going to distribute it any time in the future it’s best to write it
   in so that it’s all contained in the one place.

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

The topic ‘update_post_meta array problem’ is closed to new replies.

## Tags

 * [array](https://wordpress.org/support/topic-tag/array/)
 * [multidimensional](https://wordpress.org/support/topic-tag/multidimensional/)
 * [update_post_meta](https://wordpress.org/support/topic-tag/update_post_meta/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 6 replies
 * 3 participants
 * Last reply from: [catacaustic](https://wordpress.org/support/users/catacaustic/)
 * Last activity: [13 years, 2 months ago](https://wordpress.org/support/topic/update_post_meta-array-problem/#post-3556913)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
