Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1025149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:53:05+00:00 2026-05-16T11:53:05+00:00

I need help sorting [Time] data from in this array in php. For a

  • 0

I need help sorting [Time] data from in this array in php. For a given day, the time is NOT in order.
Is there a way to sort this? Thanks.

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 ) 

Here’s my php code, thus far:

<?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);
?>

Updated: Latest sort after using suggested fix by Bill. [Time] is in order, but also need to have it sorted within [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 )
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T11:53:06+00:00Added an answer on May 16, 2026 at 11:53 am

    edit: Now that I understand your input data better, I’ve tested this script.

    First I read the data file as you do, but I collect the data field by field directly into a two-dimensional array:

    <?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;
      }
    }
    

    Next I sort the data array with a user-defined sorting function passed to usort(). Thanks to commenters for suggestions to make this function better.

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

    Now the data is sorted by date and time, so I can simply output it:

    $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>";
    

    I’ve also added some other changes for better PHP style:

    • Be consistent about indentation.
    • Use curly-braces even for a block with one statement.
    • Use htmlspecialchars() for outputting dynamic data (including field names, CSS classes, etc.).
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Need help writing a script downloads data from google insight using c# this is
I need some help with sorting a multidimensional array in php. I need the
I need help sorting through some data. Say I type piz in a searchfield.
Hey all need a lil help sorting a loop for this table out, cant
I made this function and need some help sorting out the logic flow. It
Sorting is giving me a hard time. I need help with an small example,
I need help in designing my PHP classes where I need to extend from
Need help getting Ember-Data working with Zend Rest. At first, I'm familiar with Zend
I need help with one ajax function This is raw page setup. Page will
I need help sorting and counting instances of the words in a string. Lets

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.