2017-05-16 26 views
1

我很努力让array_multisort()工作。我从排序检索JSON一些数据,是五个对象,每个这种格式的博客文章一组数据:如何使用array_multisort()对PHP中的对象数组进行排序?

"1":{"title": "It's a fixer-upper of a planet but we could make it work", 
    "post_date": "1454889600", 
    "author": "Elon Musk", 
    "content": "<p>We choose to go to the moon in this decade and do the other things...</p>", 
    "category": [ "mars", "space travel" ] },  

    "2":{"title": "Failure is not an option", 
    "post_date": "1456099200", 
    "author": "Gene Kranz", 
    "content": "<p>Dinosaurs are extinct today because ...</p>", 
    "category": [ "mis-quoted", "apollo 13" ] }, 

...等

我得到的文件在PHP中,解码将JSON转换为关联数组,然后创建一个我可以工作的可读日期数组。我有一个由五个对象组成的数组,需要按照所述日期对数组进行排序。然后我尝试使用array_multisort,似乎无法找到可用的语法。任何帮助,将不胜感激,我敢肯定,这是我小看我。无论我多么努力谷歌,我似乎无法得到正确的搜索字符串。请帮助?

<?php //This part I'm confident is working. 
    $json = file_get_contents("./data/posts.json"); 
    $json_content = json_decode($json, true); 
    $date_sort = array(); 

    //Sorting the Array - this part seems to work 
    foreach ($json_content as $postObj) { 
     $post_date_human = date ('Y-m-d', $postObj['post_date']); 
     array_push($date_sort, $post_date_human); 
    } 
    print_r ($date_sort); //Seems to be working fine, now to try to sort one array of objects by the position of dates in the second array 

    // Wai u no werk!? 
    array_multisort($json_content, $date_sort = SORT_ASC); 
    print_r ($json_content); 
+1

对不起。你真的想做什么? – Nidhi

+0

我想使用array_multisort()方法按发布日期排序博客文章,最近到最早。 :) –

+0

在下面阅读你的文章看起来像你自己解决了...对吗? – Nidhi

回答

0

编辑:Sort multidimensional array by multiple keys这里忽略了PHP文档:阅读评论,检查出这是宝贵的像这样的其他线程后http://php.net/manual/en/function.array-multisort.php

我得到了我代码通过使用array_multisort()排序的索引数组来提供的第一个数组。同样,传递给array_multisort()的第一个参数是SORTED BY,而不是您想要排序的数组。这与PHP文档相反,但似乎工作。如果您在我的代码中发现错误解释或错误,请告诉我。在此之前,我的代码的修复最终成为这样的:

array_multisort($ date_sort,SORT_DESC,$ json_content);

它似乎按降序排列$ date_sort,先放置最新日期,然后按第一个排序方式排序第二个对象数组。我想到了像Excel这样的程序如何基于单个列对表格进行排序的想法。

感谢您花时间给我反馈并提出问题。所有这些都有助于最终弄清楚文档的措词看起来是相反的(因为排序的数组本身也被排序(当然),但这不是调用函数的意图,而是其他数组它们本身是相对于第一个数组排序的)。

0

仅供参考请参阅下面的代码。

$json_content = msort($json_content, "post_date"); 

And heres the function itself: 

/** 
* Sort a 2 dimensional array based on 1 or more indexes. 
* 
* msort() can be used to sort a rowset like array on one or more 
* headers (keys in the 2th array). 
* 
* @param array  $array  The array to sort. 
* @param string|array $key  The index(es) to sort the array on. 
* @param int   $sort_flags The optional parameter to modify the sorting 
*         behavior. This parameter does not work when 
*         supplying an array in the $key parameter. 
* 
* @return array The sorted array. 
*/ 
function msort($array, $key, $sort_flags = SORT_REGULAR) { 
    if (is_array($array) && count($array) > 0) { 
     if (!empty($key)) { 
      $mapping = array(); 
      foreach ($array as $k => $v) { 
       $sort_key = ''; 
       if (!is_array($key)) { 
        $sort_key = $v[$key]; 
       } else { 
        // @TODO This should be fixed, now it will be sorted as string 
        foreach ($key as $key_key) { 
         $sort_key .= $v[$key_key]; 
        } 
        $sort_flags = SORT_STRING; 
       } 
       $mapping[$k] = $sort_key; 
      } 
      asort($mapping, $sort_flags); 
      $sorted = array(); 
      foreach ($mapping as $k => $v) { 
       $sorted[] = $array[$k]; 
      } 
      return $sorted; 
     } 
    } 
    return $array; 
} 

更多信息请访问:https://blog.jachim.be/2009/09/php-msort-multidimensional-array-sort/comment-page-1/

相关问题