0

What array management operations are needed? So, there are two arrays and I want to combine the following way:

$arr

Array
(
    [0] => 2015-08-16 22:12:04
    [1] => 2015-08-16 13:20:17
    [2] => 2015-08-16 11:45:47
    [3] => 2015-08-16 02:35:12
    [4] => 2015-08-15 19:05:02
    [5] => 2015-08-15 17:35:12
    [6] => 2015-08-15 09:02:25
    [7] => 2015-08-15 07:12:00
    [8] => 2015-08-14 22:12:04
    [9] => 2015-08-14 13:20:17
    [10] => 2015-08-14 11:45:47
)

other array ($arr2)

Array
(
    [2015-08-16 22:12:04] => 4.8
    [2015-08-16 13:20:17] => 5.8
    [2015-08-16 11:45:47] => 4.7
    [2015-08-16 02:35:12] => 2.8
    [2015-08-15 19:05:02] => 5.0
    [2015-08-15 17:35:12] => 3.0
    [2015-08-15 09:02:25] => 5.6
    [2015-08-15 07:12:00] => 4.0
    [2015-08-14 22:12:04] => 4.8
    [2015-08-14 13:20:17] => 5.8
)

I would like the following output: $arr3

Array
(
    2015-08-16 => Array 
        (
           [2015-08-16 22:12:04] => 4.8
           [2015-08-16 13:20:17] => 5.8
           [2015-08-16 11:45:47] => 4.7
           [2015-08-16 02:35:12] => 2.8
        )
    2015-08-15 => Array 
        (
           [2015-08-15 19:05:02] => 5.0
           [2015-08-15 17:35:12] => 3.0
           [2015-08-15 09:02:25] => 5.6
           [2015-08-15 07:12:00] => 4.0
        )
    2015-08-14 => Array 
        (
           [2015-08-14 22:12:04] => 4.8
           [2015-08-14 13:20:17] => 5.8
           [2015-08-14 11:45:26] => 4.4
        )

)

So far I got: (the whole is a for loop)

$ts = strtotime($year.'W'.$week.$i);
$thedates = date("Y-m-d", $ts);

$input = preg_quote($thedates, '~');
$input = str_replace("\-","-",$input);
$result = preg_grep('~' . $input . '~', $arr);

$a = array(
   $thedates=>$result,                      
);

Output:

Array
(
    [1439510400] => Array
        (
            [8] => 2015-08-14 22:12:04
            [9] => 2015-08-14 13:20:17
            [10] => 2015-08-14 11:45:47
            [11] => 2015-08-14 02:35:12
        )
)

Array
(
    [1439596800] => Array
        (
            [4] => 2015-08-15 19:05:02
            [5] => 2015-08-15 17:35:12
            [6] => 2015-08-15 09:02:25
            [7] => 2015-08-15 07:12:00
        )
)

Array
(
    [1439683200] => Array
        (
            [0] => 2015-08-16 22:12:04
            [1] => 2015-08-16 13:20:17
            [2] => 2015-08-16 11:45:47
            [3] => 2015-08-16 02:35:12
        )
)
Rizier123
  • 58,877
  • 16
  • 101
  • 156
Stanley
  • 17
  • 1
  • 6

1 Answers1

3

A simple foreach loop will do what you want. Just explode() the key by the space, so you have date and time separated, e.g.

<?php

    $result = [];

    foreach($arr2 as $k => $v){
        list($date, $time) = explode(" ", $k);
        $result[$date][$k] = $v;
    }

?>
Rizier123
  • 58,877
  • 16
  • 101
  • 156
  • I think `$result[$date][$time] = $v;` should be `$result[$date][$k] = $v;` because @Stanley wants to have date and time as array key in the second level? – mario.van.zadel Sep 10 '15 at 08:32
  • 1
    Perfect! I was too complicated :) – Stanley Sep 10 '15 at 08:53
  • That works perfect if the first array don't needed. Vote UP! But if author wants combine arrays based on some specific values you need add additional loop that should wrap arr2. It might be needed if arr contains not all dates that are in arr2, but result should be based on arr dates. – iurii_n Sep 10 '15 at 08:53
  • @YuriyNedostup See the comments under OP's question: http://stackoverflow.com/questions/32496716/array-merge-combination-with-foreach#comment52853902_32496716 – Rizier123 Sep 10 '15 at 08:55
  • @Rizier123 ahhh, ok. Sorry. Then I'll take my words back – iurii_n Sep 10 '15 at 08:57