2017-03-16 124 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是啊,你说得对! –