如何用PHP对多维(json)数组进行排序?

时间:2021-05-09 15:55:01

Can someone help me please ... I've read previous PHP 'sorting' questions ... and tried to follow the logic ... but brain fade prevails ;-!

谁能帮帮我吗?我之前读过PHP“排序”问题……并试图遵循逻辑……但是大脑逐渐衰退;-!

I have a json object (from facebook) which I am trying to sort using one of the values ('created_time') using the following code:

我有一个json对象(来自facebook),我正在尝试使用其中一个值(“created_time”)使用以下代码对其进行排序:

$json = file_get_contents($url,0,null,null);
$result = json_decode($json, true);
array_multisort($note['created_time'], SORT_ASC, $result);   /* what's wrong with this line?? */

Example json data shown below:

示例json数据如下所示:

{
   "data": [
      {
         "id": "123",
         "from": {
            "name": "Example",
            "category": "Musician/band",
            "id": "123"
         },
         "subject": "Subject 1",
         "message": "Example message text 1",
         "created_time": "2011-07-12T20:18:17+0000",
      },
      {
         "id": "123",
         "from": {
            "name": "Example",
            "category": "Musician/band",
            "id": "123"
         },
         "subject": "Subject 2",
         "message": "Example message text 2",
         "created_time": "2011-07-12T20:21:01+0000",
      },
...

Thanks for you help.

谢谢你的帮助。

2 个解决方案

#1


2  

Your PHP snippet references a $note object but I just see the $json and $result variables...

您的PHP代码片段引用了$note对象,但我只看到了$json和$result变量……

Assuming we have the data array available you can use usort with a custom function.. this could be an anonymous function if you are using PHP 5.3+

假设我们有可用的数据数组,你可以使用usort with一个自定义函数。如果您使用的是PHP 5.3+,这可能是一个匿名函数

function sort_by_creation($a, $b) 
{
  return (strtotime($a['created_time']) < strtotime($b['created_time']) ? -1 : 1;
}
usort($data, 'sort_by_creation');

#2


0  

I'm not sure if that will work. Where does $note come from? If you are trying to sort an array by a specific member, what about usort? http://php.net/manual/en/function.usort.php

我不确定这是否行得通。$note从哪里来?如果你想对一个特定的成员排序,那么usort呢?http://php.net/manual/en/function.usort.php

so something like:

所以类似:

function sortByCreatedTime($a, $b){
  if ($strtotime($a['data']['created_time']) == strtotime($b['data']['created_time'])) {
    return 0;
  };
  return ($strtotime($a['data']['created_time']) < strtotime($b['data']['created_time'])) ? -1 : 1;
};
usort($result, sortByCreatedTime);
for($i = 0; $i < count($results['data']); $i++){
  //should output in descending order.  if not just change 
  //the < to a > in the sort function above
  echo 'time is => ' . $results['data']['created_time'] . '<br>';
};

#1


2  

Your PHP snippet references a $note object but I just see the $json and $result variables...

您的PHP代码片段引用了$note对象,但我只看到了$json和$result变量……

Assuming we have the data array available you can use usort with a custom function.. this could be an anonymous function if you are using PHP 5.3+

假设我们有可用的数据数组,你可以使用usort with一个自定义函数。如果您使用的是PHP 5.3+,这可能是一个匿名函数

function sort_by_creation($a, $b) 
{
  return (strtotime($a['created_time']) < strtotime($b['created_time']) ? -1 : 1;
}
usort($data, 'sort_by_creation');

#2


0  

I'm not sure if that will work. Where does $note come from? If you are trying to sort an array by a specific member, what about usort? http://php.net/manual/en/function.usort.php

我不确定这是否行得通。$note从哪里来?如果你想对一个特定的成员排序,那么usort呢?http://php.net/manual/en/function.usort.php

so something like:

所以类似:

function sortByCreatedTime($a, $b){
  if ($strtotime($a['data']['created_time']) == strtotime($b['data']['created_time'])) {
    return 0;
  };
  return ($strtotime($a['data']['created_time']) < strtotime($b['data']['created_time'])) ? -1 : 1;
};
usort($result, sortByCreatedTime);
for($i = 0; $i < count($results['data']); $i++){
  //should output in descending order.  if not just change 
  //the < to a > in the sort function above
  echo 'time is => ' . $results['data']['created_time'] . '<br>';
};