Sorting multi-dimensional array in PHP with multiple criteria

雨燕双飞 提交于 2020-01-14 04:27:23

问题


I'm reading a multi-dimensional array from JSON. I then need to sort based on two of the parameters, about 3 levels deep in the array.

I've tried array_multisort, but could only do one level at a time. I then moved to usort, based on several examples I saw here on stackoverflow, but it stubbornly refuses to work.

JSON:

    [
    {
    "multiple parameters": "foobar",
        "projects": [
            {
                "id": "00101",
                "date": "9",
                "time": "14:00",
          "duration":"30"
            },
            {
                "id": "EX001",
                "date": "8",
                "time": "13:30",
          "duration":"15"
            },
            {
                "id": "9A200",
                "date": "10",
                "time": "8:45",
          "duration":"15"
            },
            {
                "id": "EQ002",
                "date": "8",
                "time": "9:30",
          "duration":"15"
            }
        ]
    }
]

PHP:

//read data from the json file
$theschedule = '/directory/path/schedule.json';

//read json file
if (file_exists ($theschedule)){
    $contents = utf8_encode(file_get_contents($theschedule));
    $Schedule= json_decode($contents, true); 

//Sort array

usort($Schedule[0]['projects'], 'order_by_date_time');

function order_by_date_time($a, $b)
{
  if ($a['date'] == $b['date'])
  {
    // date is the same, sort by time
    if ($a['time'] == $b['time']) return 0;
    return $a['time'] == 'y' ? -1 : 1;
  }

  // sort the date first:
  return $a['date'] < $b['date'] ? 1 : -1;
}

Each meeting has a date and time - I need to sort by date, and then time, to populate a meeting schedule page.

I've read many, many stackoverflow posts. The most helpful were Sort multidimensional array by multiple criteria

Sort an associative array in php with multiple condition (source of the 'order_by_date_time' function)

I've read the PHP manual on usort at http://www.php.net/manual/en/function.usort.php (and many of the other array sort functions).

I've also validated the JSON with JSONLint, so I don't think it's the problem - but if that might be the problem, I can change it as well.

I know related questions have been raised here before - I've read so many of the posts, and tried so many of the suggested answers. There's some piece missing in my code, though, that I just can't see.


回答1:


This should work for you.

 function order_by_date_time($a, $b){
       if ($a["date"] == $b["date"]){
            return strtotime($a["time"]) - strtotime($b["time"]);
       } 
       return $a["date"] - $b["date"];
 }

 usort ($Schedule[0]['projects'], "order_by_date_time");

See Working Fiddle




回答2:


What about looping through all meetings, and adding an additional date_time field which is a sortable combination of both fields. Then you'd only need to sort on the one.



来源:https://stackoverflow.com/questions/18025775/sorting-multi-dimensional-array-in-php-with-multiple-criteria

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!