2017-03-16 173 views
1

嗨,我將嘗試在最近的文章中值多類型的數組,顯示數組列表的基礎上,我需要順序列表請找我的代碼,並與我們的幫助。按值排序多維數組?

<?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); 
?> 

現在顯示結果:

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 
     ) 

) 

我需要的結果(濾波[ID]使用升序列表):

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 
     ) 
) 
+0

[這堆(http://stackoverflow.com/questions/2699086/sort-multi-dimensional-array-by-value)答案將解決您的問題。 –

回答

1

使用usort對該數組進行排序。您可以檢查simple live demo here

usort($array, function($a, $b){return $b['id'] - $a['id'];}); 
+1

在PHP7可以使用飛船操作 – bxN5

+0

@ bxN5是啊,你說得對! –