按子键日期对多维数组进行排序?

发布于 2024-11-02 22:28:27 字数 650 浏览 0 评论 0原文

我有以下数组:

[0] => Array
        (
            [name] => The Name
            [description] => description
            [date] => Thur, May 5 @ 7:00 p.m.
            [rating] => PG
            [year] => 2011
        )

[1] => Array
        (
            [name] => Name 2
            [description] => description 2
            [date] => Sun, May 8 @ 7:30 p.m.
            [rating] => 14A
            [year] => 2011
        )

还有大约 5-10 个部分。

我最终想做的是使用数组的日期部分按天对这些项目进行分组(即,“所有“日期”落入“5 月 8 日”的项目都应如此分组)。

知道我如何请注意,“日期”字段是这样存储在数据库中的——这不是从 date() 或其他内容转换而来的时间戳

I have the following array:

[0] => Array
        (
            [name] => The Name
            [description] => description
            [date] => Thur, May 5 @ 7:00 p.m.
            [rating] => PG
            [year] => 2011
        )

[1] => Array
        (
            [name] => Name 2
            [description] => description 2
            [date] => Sun, May 8 @ 7:30 p.m.
            [rating] => 14A
            [year] => 2011
        )

There are about 5-10 more parts.

What I'm ultimately wanting to do is to use the date part of the array to group these items by day (I.e., "all items with "date" falling into "May 8" should be grouped as such).

Any idea how I'd go about this? Note that the "date" field is stored as such in DB -- that's not a timestamp converted from date(); or whatever.

Many thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

溺渁∝ 2024-11-09 22:28:27

创建您自己的排序函数并使用 usort 调用它。

例如(不考虑时间戳格式的复杂性):

function date_sort($a, $b) {
  return strcmp($a['date'], $b['date']); //only doing string comparison
}

usort($array, 'date_sort');

要完成 date_sort,您需要以某种方式将日期转换为可比较的类型。这是将它们转换为 UNIX 时间戳的解决方案:

function convert_date($time) {
  $time = substr($time, strpos($time, ',')+1);
  $time = str_replace('@', ',', $time);
  $time = str_replace('.', '', $time);
  return strtotime($time);
}

function date_sort($a, $b) {
  $a = convert_date($a['date']);
  $b = convert_date($b['date']);

  if ($a == $b) {
    return 0;
  }
  return ($a < $b) ? -1 : 1;
}

Create your own sort function and call it using usort.

For example (not considering the intricacies of your timestamp format):

function date_sort($a, $b) {
  return strcmp($a['date'], $b['date']); //only doing string comparison
}

usort($array, 'date_sort');

To complete date_sort, you'll somehow need to convert the dates to comparable types. Here's a solution which converts them to UNIX timestamps:

function convert_date($time) {
  $time = substr($time, strpos($time, ',')+1);
  $time = str_replace('@', ',', $time);
  $time = str_replace('.', '', $time);
  return strtotime($time);
}

function date_sort($a, $b) {
  $a = convert_date($a['date']);
  $b = convert_date($b['date']);

  if ($a == $b) {
    return 0;
  }
  return ($a < $b) ? -1 : 1;
}
面犯桃花 2024-11-09 22:28:27

使用@Emil Vikström解决方案,用strtotime作为比较函数

function date_sort($a, $b) {

$a = strtotime($a['date']);
$b = strtotime($b['日期']);

返回($a==$b)? 0 : ($a>$b ? - 1 : 1);

}

usort($array, 'date_sort');

https://www.php.net/manual/en/function.strtotime。 php 应该处理大多数用英语编写的文本日期。

Use @Emil Vikström solution, with strtotime as comparing function

function date_sort($a, $b) {

$a = strtotime($a['date']);
$b = strtotime($b['date']);

return ($a == $b) ? 0 : ($a>$b ? - 1 : 1);

}

usort($array, 'date_sort');

https://www.php.net/manual/en/function.strtotime.php should deal with most textual dates written in english.

静赏你的温柔 2024-11-09 22:28:27

试试这个

$arrArray 有你指定的数组;

$newArray = array();
foreach($arrArray as $key => $value){

    $date1 = substr($value['date'], 0, strpos($value['date'],'@')-1);
    $newArray[$date1][] = $value;


}
print_r($newArray);

Try this

$arrArray has array you have specified;

$newArray = array();
foreach($arrArray as $key => $value){

    $date1 = substr($value['date'], 0, strpos($value['date'],'@')-1);
    $newArray[$date1][] = $value;


}
print_r($newArray);
贪恋 2024-11-09 22:28:27

要按日期对数据进行分组,请运行这个小循环

$sortedArray = array();
foreach($dataListing as $data){
     //parse your $date field with an reg exp and transform it into integer with format MMDD
     $sortedArray[$dateIntegerFormated][] = $data;
}
ksort($sortedArray);
$sortedArray = array_values($sortedArray);

,您可以使用 usort 按时间对每个组进行排序

For grouping your data by date, run this little loop

$sortedArray = array();
foreach($dataListing as $data){
     //parse your $date field with an reg exp and transform it into integer with format MMDD
     $sortedArray[$dateIntegerFormated][] = $data;
}
ksort($sortedArray);
$sortedArray = array_values($sortedArray);

And you can use usort for sorting by time into each group

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文