How to sum the point fields of all arrays, theme php code

Support MB Builder How to sum the point fields of all arrays, theme php code

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #43472
    369cycle369cycle
    Participant


    <?php
    $groups = rwmb_meta( 'group_point', [ 'object_type' => 'user' ], get_current_user_id() );
    foreach ( $groups as $group ) {
    	
    	// Field date_point:
    	echo $group[ 'date_point' ] ?? '';
    	
    	// Field name_point:
    	echo $group[ 'name_point' ] ?? '';
    	
    	// Field number_point:
    	echo $group[ 'number_point' ] ?? '';
    
    }
    ?>
    #43473
    369cycle369cycle
    Participant

    sum
    sum

    #43474
    369cycle369cycle
    Participant
    #43488
    PeterPeter
    Moderator

    Hello,

    You can use two PHP functions array_push() and array_sum() to sum the number field value in a cloneable group. For example:

    $groups = rwmb_meta( 'group_point', [ 'object_type' => 'user' ], get_current_user_id() );
    $total_points = [];
    foreach ( $groups as $key => $group ) {
            // Field number_point:
    	array_push( $total_points, $group[ 'number_point' ] );
    }
    
    echo array_sum( $total_points );
Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.