0

I am getting this from my select statement:

Array
(
[0] => Array
    (
        [time] => 2013-11-06 09:15:00
        [type] => 1
    )

[1] => Array
    (
        [time] => 2013-11-06 11:00:00
        [type] => 2
    )

[2] => Array
    (
        [time] => 2013-11-06 11:15:00
        [type] => 3
    )

[3] => Array
    (
        [time] => 2013-11-06 13:00:00
        [type] => 4
    )

[4] => Array
    (
        [time] => 2013-11-06 13:29:00
        [type] => 5
    )

[5] => Array
    (
        [time] => 2013-11-06 15:00:00
        [type] => 6
    )

[6] => Array
    (
        [time] => 2013-11-06 15:15:00
        [type] => 7
    )

[7] => Array
    (
        [time] => 2013-11-06 17:00:00
        [type] => 8
    )

[8] => Array
    (
        [time] => 2013-11-07 09:00:00
        [type] => 1
    )

[9] => Array
    (
        [time] => 2013-11-07 10:15:00
        [type] => 2
    )

[10] => Array
    (
        [time] => 2013-11-07 10:30:00
        [type] => 3
    )

[11] => Array
    (
        [time] => 2013-11-07 13:00:00
        [type] => 4
    )

[12] => Array
    (
        [time] => 2013-11-07 13:30:00
        [type] => 5
    )

[13] => Array
    (
        [time] => 2013-11-07 14:30:00
        [type] => 6
    )

[14] => Array
    (
        [time] => 2013-11-07 14:45:00
        [type] => 7
    )

[15] => Array
    (
        [time] => 2013-11-07 17:00:00
        [type] => 8
    )

)

What I want to do is group the arrays by date and set each time value as an element of that array, but change the key from [time] to the number value assigned to [type].

So I'd get something like this:

$records[] = array();

$records[2013-11-06] => Array
    ( [1] => 09:15:00
      [2] => 11:15:00 
      [3] => 11:15:00 
      etc.
    )
$records[2013-11-07] => Array
    ( [1] => 09:00:00
      [2] => 10:15:00 
      [3] => 10:30:00
      etc.
    )

Any help is GREALY appreciated, as I am STUCK! Thank you!

3
  • 1
    Please, tell which database management system (DBMS) you are using. Sometimes you can solve the problem rewriting the query. :) Commented Nov 7, 2013 at 23:42
  • 1
    it's simple. Just try. Commented Nov 7, 2013 at 23:46
  • You should include any attempts you have tried (and failed) with as well Commented Nov 7, 2013 at 23:54

3 Answers 3

1

Assuming that you're using MySQL, you can just rewrite your query like this:

  SELECT DATE(time) AS date, GROUP_CONCAT(TIME(time) ORDER BY time) AS time
    FROM table_name
GROUP BY date

And you'll get your data like that:

|------------|-------------------------------------------------------------------------|
| date       | time                                                                    |
|------------|-------------------------------------------------------------------------|
| 2013-11-06 | 09:15:00,11:00:00,11:15:00,13:00:00,13:29:00,15:00:00,15:15:00,17:00:00 |
| 2013-11-07 | 09:00:00,10:15:00,10:30:00,13:00:00,13:30:00,14:30:00,14:45:00,17:00:00 |
|------------|-------------------------------------------------------------------------|

From that you can simply use PHP explode() function to get all time values of each record as an array.

Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

$records = array();
foreach($yourarray as $arr)
  {
  list($date,$time)=explode(' ',$arr['time']);
  $records[$date][$arr['type']]=$time;
  }

Comments

0

You can just iterate over your rows and assign the needed values.

$records = array();
foreach ($rows as $row) {
    $timestamp = strtotime($row['time']);
    $date = date("Y-m-d", $timestamp);
    $time = date("H:i:s", $timestamp);
    if (!is_array($records[$date])) $records[$date] = array();
    $records[$date][ $row['type'] ] = $time;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.