Sort results from high to low float array – wordpress

Tags: , , ,



Maybe this should be very easy but I am a bit lost on how to sort this data in a good way.

I got this query in a WordPress template to get some post_meta:

                        $args = array( 
                        'post_type' => 'page',
                        'fields' => 'ids',
                        'nopaging' => true,
                        'meta_query' => array(
                        array(
                            'key' => '_wp_page_template',
                            'value' => array('page-provider.php'),
                            'compare' => 'IN',
                        ),
                            'prijs_sort' => array(
                                'key' => 'rwp_user_score',
                                'type' => 'DECIMAL(10,2)',
                                'compare' => 'EXISTS'
                            ),
                        ),

                    );

With the data I get from this query get some more info and I want to final result sorted from High to Low.

                $pages = get_posts( $args );
                foreach ( $pages as $page ) {
                    $get_average_score = get_post_meta( $page, 'rwp_user_score', true );
                    $gemiddelde_score = round($get_average_score, 1);

                    $get_review_count = get_post_meta( $page, $key = 'rwp_rating_0' , $single = false);
                    $get_review_count_aantal = array_count_values(array_column($get_review_count, 'rating_post_id'))[$page];

                    $name_provider = get_post_meta( $page, 'bedrijfsnaam_hosting_provider', false );
                    $gemiddeld_totaal = $gemiddelde_score * $get_review_count_aantal / 100;
                    echo implode(', ', $name_provider);
                    echo $gemiddeld_totaal;
                    var_dump($gemiddeld_totaal);
                    echo "<br/>";
                }
                wp_reset_postdata();

I got $name_provider; and $gemiddeld_totaal; and I want the results to orderby $gemiddeld_totaal; from high to low. I tried a bunch of things but no luck so far. It is a float array and for some reason I am not able to sort the results.

This is a part of the var_dump of $gemiddeld_totaal;:

Page_title_1 0.05float(0.05) 
Page_title_2 0.784float(0.784) 
Page_title_3 0.144float(0.144) 
Page_title_4 0.066float(0.066) 
Page_title_5 0.048float(0.048) 
Page_title_6 0.048float(0.048) 

How can I sort the results by $gemiddeld_totaal;? And the field $name_provider; also should sort with the sorting of $gemiddeld_totaal;.

EDIT The results shoud look like:

Name provider 1  - Average Total 1
Name provider 2  - Average Total 2
Name provider 3  - Average Total 3

EDIT 2 (adding one more data field) In de foreach ( $pages as $page ) { I got one more data field that is needed in the output of the other foreach.

$shortcode = do_shortcode("[rwp_users_rating_stars id='0' post="$page"]");

I tried some things but cant seem to figure out how to add this. This is the new line in the foreach. Could you explain me how to do this? Maybe I will understand it better then.

Answer

One way of doing this is using array_multisort() the documentation says:

We have an array of rows, but array_multisort() requires an array of columns

$name_providers = array();
$gemiddelden_totaal = array();

foreach ( $pages as $page ) {
    $get_average_score = get_post_meta( $page, 'rwp_user_score', true );
    $gemiddelde_score = round($get_average_score, 1);
    $get_review_count = get_post_meta( $page, $key = 'rwp_rating_0' , $single = false);
    $get_review_count_aantal = array_count_values(array_column($get_review_count, 'rating_post_id'))[$page];
    $name_provider = get_post_meta( $page, 'bedrijfsnaam_hosting_provider', false );
    $gemiddeld_totaal = $gemiddelde_score * $get_review_count_aantal / 100;
    $name_providers[] = implode(', ', $name_provider); //we'll use this one as a column
    $gemiddelden_totaal[] = $gemiddeld_totaal; //we'll use this one as a column
    $data[] = array('name_provider' => implode(', ', $name_provider), 'gemiddeld_totaal' => $gemiddeld_totaal); //this one contains our rows
}

Once you’ve got that part you can sort both array’s like:

$name_providers  = array_column($data, 'name_provider');
$gemiddelden_totaal = array_column($data, 'gemiddeld_totaal');
array_multisort($gemiddelden_totaal, SORT_DESC, $name_providers, SORT_ASC, $data);

edit To get the desired result you could use something like :

foreach($gemiddelden_totaal as $key => $gemiddelde){
    echo $name_providers[$key] . '  - '  . $gemiddelde;
}


Source: stackoverflow