Cannot Sum Array From Custom Taxonomy
-
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
- The topic ‘Cannot Sum Array From Custom Taxonomy’ is closed to new replies.