Title: Using update_user_meta with an array
Last modified: January 28, 2018

---

# Using update_user_meta with an array

 *  [jd-fb](https://wordpress.org/support/users/jd-fb/)
 * (@jd-fb)
 * [8 years, 4 months ago](https://wordpress.org/support/topic/using-update_user_meta-with-an-array/)
 * I’m building a plugin where I’m trying to use a user meta field to store a history
   of user activity. I’m having a hard time wrapping my head around how to update
   and store the array of data.
 * So, my question is: can somebody tell how to update my code? Or, would it be 
   simpler to create a table with this data? Create a new post type?
    (I thought
   a user meta field would be better, so that I wouldn’t clutter the database with
   another table, but maybe my reasoning is off.)
 * Anyway, here’s what I’m trying to accomplish:
    1 – In the front end, the user
   chooses from a drop-down menu their selection for the day. For example: “Today,
   I ate an APPLE or ORANGE or BANANA”. 2 – The plugin saves the vote of fruit for
   the day in the user meta field I call `myplugin-hist`. 3 – The next day, if the
   user chooses a new fruit, the plugin adds this new fruit to the `myplugin-hist`
   so that we can see what the choice was yesterday and again today. 4 – I’ve tried
   various ways of storing the data, and I get arrays within arrays, not resembling
   the structure I have in mind, which should ideally have this structure:
 *     ```
       array(
       	day 1 => apple
       	day 2 => banana
       	day 3 => apple
       )
       ```
   
 * Here’s a sample of my code. So far, I’ve written just the code to save to the
   database (which is where I need help.)
 *     ```
       // This function receives the (int)$fruit vote from another function
       // (apple is 1, banana is 2, etc)
       public function i_vote_for($fruit) {
       	global $MainPluginClass;
   
       	// First, we obtain the user's vote history
       	$hist = get_user_meta($this->ID, 'myplugin-hist', false);
   
       	// I added this, in case it's a brand-new user with no history
       	if (!array($hist)) $hist = array();
   
       	// This is a custom function that returns an int, to count day 1, day 2, etc
       	$d = $MainPluginClass->current_date();
   
       	//This is the part where I'm scrathcing my head.
       	//I've tried $hist[$d][] = $fruit;
       	//I've tried $hist[] = array ($d, fruit);
       	$hist[0][] = $fruit;		
       	update_user_meta($this->ID, 'myplugin-hist', $hist);     
       	// the $this->ID is a shorthand in my plugin for get_current_user...       
       }
       ```
   
    -  This topic was modified 8 years, 4 months ago by [jd-fb](https://wordpress.org/support/users/jd-fb/).

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

 *  [Soumanta Bhowmick](https://wordpress.org/support/users/soumanta/)
 * (@soumanta)
 * [8 years, 4 months ago](https://wordpress.org/support/topic/using-update_user_meta-with-an-array/#post-9909802)
 * No need to explicitly fetch an array, you can do it like this
    `$hist = get_user_meta(
   $this->ID, 'myplugin-hist', true);` or else the meta value will be wrapped in
   another array.
 * This is not the condition you want to check,
    `if (!array($hist))` change it 
   to, `if (!is_array($hist))`
 * Finally, store your value like this,
    `$hist[$d] = $fruit;`
 * Lets put this all together,
 *     ```
       // This function receives the (int)$fruit vote from another function
       // (apple is 1, banana is 2, etc)
       public function i_vote_for($fruit) {
       	global $MainPluginClass;
   
       	// First, we obtain the user's vote history
       	$hist = get_user_meta($this->ID, 'myplugin-hist', true);
   
       	// I added this, in case it's a brand-new user with no history
       	if (!is_array($hist)) $hist = array();
   
       	// This is a custom function that returns an int, to count day 1, day 2, etc
       	$d = $MainPluginClass->current_date();
   
       	//This is the part where I'm scrathcing my head.
       	$hist[$d] = $fruit;		
       	update_user_meta($this->ID, 'myplugin-hist', $hist);     
       	// the $this->ID is a shorthand in my plugin for get_current_user...       
       }
       ```
   
    -  This reply was modified 8 years, 4 months ago by [Soumanta Bhowmick](https://wordpress.org/support/users/soumanta/).
 *  Thread Starter [jd-fb](https://wordpress.org/support/users/jd-fb/)
 * (@jd-fb)
 * [8 years, 4 months ago](https://wordpress.org/support/topic/using-update_user_meta-with-an-array/#post-9911305)
 * Thanks for the info.
    I do a var_dump of the array after saving it, and I’m still
   getting variations of
 *     ```
       array(
       	array(
       		day 1=>apple
       		day 2=>banana
       		day 3=>orange
       	)
       )
       ```
   
 * (An array inside of an array. This makes it difficult to use foreach to iterate
   through the data.)
 * I’ve just re-read the Codex for `add_user_meta`, and it looks like I could use
   that to add multiple lines to the database with my myplugin-hist meta key. If
   I use `add_user_meta` with $unique set to false, then WP adds my meta key as 
   many times as I need.
 * I think I’ll try that to see how it works.

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

The topic ‘Using update_user_meta with an array’ is closed to new replies.

## Tags

 * [array](https://wordpress.org/support/topic-tag/array/)
 * [get_user_meta](https://wordpress.org/support/topic-tag/get_user_meta/)
 * [table](https://wordpress.org/support/topic-tag/table/)
 * [update_user_meta](https://wordpress.org/support/topic-tag/update_user_meta/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 2 replies
 * 2 participants
 * Last reply from: [jd-fb](https://wordpress.org/support/users/jd-fb/)
 * Last activity: [8 years, 4 months ago](https://wordpress.org/support/topic/using-update_user_meta-with-an-array/#post-9911305)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
