1

Hi i will try multi sort array , display array list base on recent post values, i need order list please find my code and help with us.

<?php
 $data = array();
    $i = 0;
    while($loop->have_posts()) : $loop->the_post();
    $title = get_the_title();
    $large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'small');
    $comments_count = wp_count_comments(get_the_ID ());
    $comments_count = $comments_count->total_comments;

    ?>
        <div class="load_more" style="color:red;">
            <?php $row_id =  echo_views($id);
            $data[$i]['id'] = $row_id;
            $data[$i]['title'] = $title;
            $data[$i]['image'] = $large_image_url;
            $data[$i]['comments'] = $comments_count;            
            ?>
        </div>
    <?php    
    $i++;
    endwhile;
echo '<pre>';print_r($data);
?>

Now showing result :

Array
(
    [0] => Array
        (
            [id] => 127
            [title] => test2
            [image] => Array
                (                    
                )
            [comments] => 0
        )

    [1] => Array
        (
            [id] => 116
            [title] => test3
            [image] => Array
                (                    
                )
            [comments] => 0
        )
    [2] => Array
        (
            [id] => 124
            [title] => test2
            [image] => Array
                (                    
                )
            [comments] => 0
        )

)

I need result (filter [id] using ascending order list):

Array
(
    [0] => Array
        (
            [id] => 127
            [title] => test2
            [image] => Array
                (                    
                )
            [comments] => 0
        )

    [1] => Array
        (
            [id] => 124
            [title] => test2
            [image] => Array
                (                    
                )
            [comments] => 0
        )

    [2] => Array
        (
            [id] => 116
            [title] => test3
            [image] => Array
                (                    
                )
            [comments] => 0
        )
)
LF00
  • 27,015
  • 29
  • 156
  • 295
Kannan
  • 347
  • 2
  • 13
  • [This stack](http://stackoverflow.com/questions/2699086/sort-multi-dimensional-array-by-value) answer will solve your problem. – Hiren Makwana Mar 16 '17 at 10:08

2 Answers2

2

use array_multisort array.

PHP reference

array_multisort and array_column function can be used to sort multi dimensional array using the key,which is ID in your example.array_column function is used to specify the key which is used as the sort order.

syntax:

array_multisort(array_column(array_name,key_name),SORT_DESC/SORT_ASC,array_name);

Solution:

array_multisort(array_column($data,'id'),SORT_ASC,$data);
1

use usort to sort the array. You can check the simple live demo here.

usort($array, function($a, $b){return $b['id'] - $a['id'];});
LF00
  • 27,015
  • 29
  • 156
  • 295