帮助排序键

时间:2010-08-23 21:33:40

标签: php sorting

我需要帮助在php中对此数组中的[Time]数据进行排序。在某一天,时间不合适。 有没有办法对此进行排序?感谢。

Array ( [0] => Array ( )
[1] => Array (         
               [Server] => server1.name 
               [Date] => Sun Aug 22 2010         
               [Set] => db2.bak_lvm         
               [Time] => 06:00:02         
               [Duration] => 01:28:12         
               [Size] => 72.05 GB         
               [Status] => Succeeded ) 
[2] => Array ( [Server] => server1.name                
               [Date] => Sun Aug 22 2010 
               [Set] => db2.bak_lvm 
               [Time] => 00:00:03 
               [Duration] => 01:49:37 
               [Size] => 187.24 GB 
               [Status] => Succeeded ) 
[3] => Array ( [Server] => server1.name 
               [Date] => Sun Aug 22 2010 
               [Set] => db3.bak_lvm 
               [Time] => 23:00:03 
               [Status] => Unsuccessful ) 
[4] => Array ( [Server] => server1.name 
               [Date] => Sun Aug 22 2010 
               [Set] => db4.bak_lvm  
               [Time] => 04:00:03 
               [Duration] => 00:42:36 
               [Size] => 46.46 GB 
               [Status] => Succeeded ) 

到目前为止,这是我的PHP代码:

<?php
$data = array();
$InputFile = file("test.txt");
foreach ($InputFile as $line){
    preg_match_all("/([0-9])-([^=]+)=([^;]+);/", $line, $matches, PREG_SET_ORDER);

    $LineData = array();
    foreach ($matches as $information)
       $LineData[$information[2]] = $information[3];
       $data[] = $LineData;
}
    $keys = array('Server', 'Date','Set','Time','Duration','Size','Status');
        echo '<table id="stats"><tr>';
        foreach ($keys as $column)
           echo '<th>' . $column . '</th>';
            echo '</tr>';

        $counter=0;
        foreach ($data as $row){
           $counter ++;
           $class = $counter % 2 === 0 ? 'alt1' : 'alt2';
           echo '<tr class="' . $class . '">';

             foreach ($keys as $column)
                if (isset($row[$column])){
                  echo '<td>' . $row[$column];
                  } else {
                  echo '<td>' . '' . '</td>';
                }
        }
        echo '</table>';
        print_r($data);
?>

已更新:使用Bill建议的修正后的最新排序。 [Time]是有序的,但也需要在[Date]

中排序
Array ( [0] => Array (
            [Server] => server1.name
            [Date] => Mon Aug 23 2010
            [Set] => db2.bak_lvm
            [Time] => 00:00:03
            [Duration] => 01:50:24
            [Size] => 187.24 GB
            [Status] => Succeeded )
    [1] => Array ( [Server] => server1.name
                   [Date] => Mon Aug 23 2010
                   [Set] => db3.bak_lvm
                   [Time] => 04:00:02
                   [Duration] => 00:42:28
                   [Size] => 46.47 GB
                   [Status] => Succeeded )
    [2] => Array ( [Server] => server1.name
                   [Date] => Sun Aug 22 2010
                   [Set] => db3.bak_lvm
                   [Time] => 04:00:03
                   [Duration] => 00:42:36
                   [Size] => 46.46 GB
                   [Status] => Succeeded )
    [3] => Array ( [Server] => server1.name
                   [Date] => Mon Aug 23 2010
                   [Set] => db1.bak_lvm
                   [Time] => 06:00:02
                   [Duration] => 01:28:24
                   [Size] => 72.05 GB
                   [Status] => Succeeded )
    [4] => Array ( [Server] => server1.name
                   [Date] => Sun Aug 22 2010
                   [Set] => db4.bak_lvm
                   [Time] => 20:00:03
                   [Duration] => 04:17:57
                   [Size] => 426.60 GB
                   [Status] => Succeeded )

3 个答案:

答案 0 :(得分:2)

编辑现在,我更了解您的输入数据,我已经测试了这个脚本。

首先我像你一样读取数据文件,但是我按字段直接将数据字段收集到一个二维数组中:

<?php

$data = array();
$InputFile = file("test.txt");
foreach ($InputFile as $line)
{
  preg_match_all("/([0-9])-([^=]+)=([^;]+);/", $line, $matches, PREG_SET_ORDER);

  foreach ($matches as $information)
  {
    $id = $information[1];
    $field = $information[2];
    $value = $information[3];
    $data[$id][$field] = $value;
  }
}

接下来,我使用传递给usort()的用户定义排序函数对数据数组进行排序。感谢评论者提出的建议,以使这项功能更好。

function comparebydatetime($a, $b) {
  $adate = strtotime($a["Date"]." ".$a["Time"]);
  $bdate = strtotime($b["Date"]." ".$b["Time"]);
  return $adate-$bdate;
}

usort($data, "comparebydatetime");

现在数据按日期排序,所以我只需输出它:

$keys = array("Server", "Date","Set","Time","Duration","Size","Status");
echo "<table id='stats'>\n";
echo "<tr>\n";
foreach ($keys as $column)
{
  echo "<th>" . htmlspecialchars($column) . "</th>\n";
}
echo "</tr>\n";

$counter=0;
foreach ($data as $row)
{
  $counter ++;
  $class = $counter % 2 === 0 ? "alt1" : "alt2";
  echo "<tr class='" . htmlspecialchars($class) . "'>\n";

  foreach ($keys as $column)
  {
    echo "<td>";
    if (isset($row[$column]))
    {
      echo htmlspecialchars($row[$column]);
    }
    echo "</td>\n";
  }
  echo "</tr>\n";
}
echo "</table>";

我还为更好的PHP样式添加了一些其他更改:

  • 对缩进保持一致。
  • 即使对于带有一个语句的块,也可以使用花括号。
  • 使用htmlspecialchars()输出动态数据(包括字段名称,CSS类等)。

答案 1 :(得分:2)

如果您已使用PHP5.3,则可以使用堆(manual | wiki):

class SortByDateTimeDescending extends SplMaxHeap
{
    public function compare($a, $b)
    {
        if(!empty($a['Date']) && !empty($a['Time']) &&
           !empty($b['Date']) && !empty($b['Time']))
        {
            $timestampA = strtotime("{$a['Date']} {$a['Time']}");
            $timestampB = strtotime("{$b['Date']} {$b['Time']}");
            return $timestampA - $timestampB;
        }
        return 0;
    }
}

$sorter = new SortByDateTimeDescending;
array_map(array($sorter, 'insert'), $data);

然后您可以{$ 1}超过$ sorter,或者,如果您希望堆返回数组,请使用

foreach

如果您尚未使用PHP5.3,则可以使用比较功能中的代码来$sortedData = iterator_to_array($sorter); 数据。订单将被颠倒(意味着升序)。迭代排序的数组很容易。

您也可以使用SortingIterator直接检查数组。

答案 2 :(得分:0)

简单地说:遍历你的数组并将每个日期作为每个子项的键,然后ksort()得到的数组。 如果有必要,请不要忘记在将日期设置为数组键之前在DateTime中翻译日期,以便ksort()可以完成它:

$to_be_sorted = array();
foreach($array as $child) {
     $date = new DateTime($child["date"].' '.$child["time"]);
     $to_be_sorted[$date] = $child;
}
$sorted = ksort($to_be_sorted);

你会丢失原来的密钥(0,1,...,4),但我认为这不是问题吗?