Title: SUM items from Array?
Last modified: August 21, 2016

---

# SUM items from Array?

 *  [Sarah_Frantz](https://wordpress.org/support/users/sarah_frantz/)
 * (@sarah_frantz)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/sum-items-from-array/)
 * I’m trying to add up all the comment meta for a particular meta key for a particular
   post. I feel like I’m close, but for some reason my output is always either ‘
   ArrayArrayArrayArrayArray’ or 1010101010, but I can never seem to get all those
   10’s to add up. Here is what I have currently:
 *     ```
       <?php
       $postid = get_the_ID(); // get id of current post
       $args = array(
       	'post_id'      => $postid,
       	'count'        => false,
       	'meta_key'     => 'price',
       	'meta_value'   => '',
       	'meta_query'   => ''
       );
   
       // The Query
       $comments_query = new WP_Comment_Query;
       $comments = $comments_query->query( $args );
   
       // Comment Loop
       if ( $comments ) {
       	foreach ($comments as $comment) {
       	$rating = array($comment->meta_value);
       	echo array_sum($rating);}
       } else {
       	 echo 'no price rating to calculate';
       	}
       ?>
       ```
   
 * >How do I get the $rating to be the added total of all the $comment->meta_value

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

 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/sum-items-from-array/#post-3731817)
 * Try it with this:
 *     ```
       // Comment Loop
       if ( $comments ) {
       	$rating = 0;
       	foreach ( $comments as $comment ) {
       		$rating += absint( $comment->meta_value ); // make sure it's an integer
       	}
       	echo ( $rating > 0 ) ? $rating : 'no ratings yet';
       } else {
       	echo 'no price rating to calculate';
       }
       ```
   
 * Or with this:
 *     ```
       // Comment Loop
       if ( $comments ) {
       	$meta_value = wp_list_pluck( $comments, 'meta_value' ); // get all meta_values
       	$meta_value = array_map( 'intval', (array) $meta_value ); // make sure they are all integers
       	$meta_value = array_sum( $rating );
       	echo ( $rating > 0 ) ? $rating : 'no ratings yet';
       } else {
       	echo 'no price rating to calculate';
       }
       ```
   
 *  Thread Starter [Sarah_Frantz](https://wordpress.org/support/users/sarah_frantz/)
 * (@sarah_frantz)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/sum-items-from-array/#post-3731825)
 * Thanks, I actually just got it solved before I saw this – but ended up doing 
   something a little different. This looks much cleaner than my code though haha
 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/sum-items-from-array/#post-3731839)
 * No problem. I’m glad you found your own solution 🙂

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

The topic ‘SUM items from Array?’ is closed to new replies.

## Tags

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 3 replies
 * 2 participants
 * Last reply from: [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * Last activity: [13 years, 1 month ago](https://wordpress.org/support/topic/sum-items-from-array/#post-3731839)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
