• jamesthemonkeh

    (@jamesthemonkeh)


    Hello

    I’m trying to get data that I’ve set with a custom taxonomy, then manipulate it.

    Retrieving the data works fine:

    global $post;
    $args = [
        'post_type' => 'post',
        'tax_query' => [
            'relation' => 'AND',
            [
                'taxonomy'         => 'yearofvisit',
                'field'            => 'name',
                'terms'            => [2020],
                'include_children' => false
            ]
        ]
    ];
    $query = new WP_Query($args);
    if ( $query->have_posts() ) {
    while ( $query->have_posts() ) : $query->the_post();
    $my_array[]=get_the_term_list( $post->ID, 'rating', '' );
    endwhile;
    }
    print_r($my_array);

    This prints out:

    Array ( [0] => 8.25 [1] => 7.15 [2] => 6.98 [3] => 6.24 [4] => 6.18 )

    However, when I then try to sum it, I get 0.

    array_sum($my_array)

    I then printed the type out and they are all strings, so I tried to convert them to numbers, yet they are still strings.

    foreach ($my_array as $value) {
        $value = floatval($value);
    };

    I did successfully convert them to double types doing something else (sorry forgot what I wrote for that) but this then set all the numbers to 0.

    Really I need to amend my $my_array array to be all numbers. Or perhaps something else? I know what I need to do were it JavaScript! JS is my day job…PHP is something I rarely touch.

    Thanks
    James

Viewing 5 replies - 1 through 5 (of 5 total)
  • Joy

    (@joyously)

    I’ve never used array_sum(), so I looked it up. There is a note at the bottom of my manual saying

    Note: PHP versions prior to 4.2.1 modified the passed array itself and converted strings to numbers (which most of the time converted them to zero, depending on their value).

    But the newer manual doesn’t have that note.
    You usually don’t have to convert the type but you didn’t say what version of PHP you are on, so you might.
    Your foreach loop didn’t actually change the array, but you could as easily have used the loop to add them up yourself.

    Values in meta fields will be saved as strings, regardless. It’s how the database schema is defined.

    Moderator bcworkz

    (@bcworkz)

    Are you able to update PHP to a recent version? In v7.2.18 I tried

    $my_array = array('8.25','7.15','6.98','6.24','6.18'); //your values as strings
    echo 'sum($my_array) = ' . array_sum($my_array) . "\n";

    The output is: sum($my_array) = 34.8
    The final 0 is dropped in output, but you do get greater precision if the sum is not an even tenth or you use printf().

    If you can’t update, I suppose you’d need to sum the values yourself in a foreach loop as Joy suggested.

    Thread Starter jamesthemonkeh

    (@jamesthemonkeh)

    Thanks for the responses. I am using PHP 7.1.9 so this should be fine.

    So I tried creating a new array and converting strings, using:

    $arr2 = array_map(function($value) {
        return floatval($value);
    }, $my_array);

    This resulted in an array of 0’s:

    Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 [4] => 0 )

    I cannot understand why. My assumption is that it is data-related and the code is fine, as if I change my original array to be an array of strings, as @bcworkz above, then the sum works.

    Moderator bcworkz

    (@bcworkz)

    floatval() can return 0 when passed strings with non-numeric content. As Joy noted, we generally don’t need to convert data types in PHP. I only used strings to show an example of this. An array constructed of the same floating numbers yields the same results.

    $my_array = array(8.25, 7.15, 6.98, 6.24, 6.18);
    echo 'sum($my_array) = ' . array_sum($my_array) . "\n";

    I suspect any manually constructed array of data will work, there’s something unexpected in the array constructed from get_the_term_list() return values.

    A bit off topic and not really my business, but saving float values as term names seems rather ill considered, especially if a post’s rating is subject to change. A floating number rating seems like it should be saved as a post’s meta value instead of being assigned as a taxonomy term. I’d consider making the effort to convert to a more logical data schema if it were my site. Just sayin’

    Thread Starter jamesthemonkeh

    (@jamesthemonkeh)

    Well I think you found the answer – adding the rating to custom fields instead of taxonomy gives me what I’m looking for!

    $my_array[]=get_field( 'rating' );

    Thanks.

    You are correct that using term names was ill considered – this was built a few years ago, before I knew any better.

    So now I have 150+ posts each using about 15 custom taxonomies. I have too many posts to convert the data manually, and cannot say I especially fancy trying to do so programmatically with close to zero database knowledge. Maybe I should?

    I also have some performance issues on editing/saving posts that use up 100% of server CPU, making website unusable for 5 minutes each time I save (or WP autosaves) and had wondered whether my mass use of taxonomies was causing the issue…not got around to debugging that…again mostly because I don’t know much away from front/end.

    Thanks again.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Cannot Sum Array From Custom Taxonomy’ is closed to new replies.