2

I have two arrays which I'm trying to merge based on the date of them. Here is what the arrays look like:

$a[0][0] = '11/15/08';
$a[0][1] = '50';
$a[1][0] = '11/20/08';
$a[1][1] = '75';
$a[2][0] = '01/04/09';
$a[2][1] = '23';
$a[3][0] = '01/09/09';
$a[3][1] = '92';

and

$b[0][0] = '01/04/09';
$b[0][1] = '30';
$b[1][0] = '01/05/09';
$b[1][1] = '54';
$b[2][0] = '01/08/09';
$b[2][1] = '89';
$b[3][0] = '01/09/09';
$a[3][1] = '62';

At the end of the merge I'm hoping for:

$n[0][0] = '11/15/08';
$n[0][1] = '50';
$n[0][2] = '0';
$n[1][0] = '11/20/08';
$n[1][1] = '75';
$n[1][2] = '0';
$n[2][0] = '01/04/09';
$n[2][1] = '23';
$n[2][2] = '30';
$n[3][0] = '01/05/09';
$n[3][1] = '0';
$n[3][2] = '54';
$n[4][0] = '01/08/09';
$n[4][1] = '0';
$n[4][2] = '89';
$n[4][0] = '01/09/09';
$n[4][1] = '92';
$n[4][2] = '62';

Is this possible?

3 Answers 3

2

I would first sort the array into one which has timestamps as its keys and arrays of numbers as its values. This will then be easy to sort. For example:

$result = array();
foreach (array_merge($a, $b) as $item) {
    $date = $item[0];
    $dateParts = explode('/', $date);
    $timestamp = mktime(0,0,0,$dateParts[0], $dateParts[1], $dateParts[2]);
    if (!isset($result[$timestamp])) {
        $result[$timestamp] = array();
    }   

    $result[$timestamp][] = $item[1];
}

Because the keys are date timestamps, we can easily sort the array by date:

ksort($result);

Then we can convert the array into the format you want it

$result2 = array();
foreach ($result as $timestamp => $item) {
    $tmp = array();

    $tmp[] = date('m/d/y', $timestamp);
    $tmp = array_merge($tmp, $item);
    $result2[] = $tmp;
}

print_r($result2);
Sign up to request clarification or add additional context in comments.

Comments

1

The easiest way is to use the date as index

    #Initialization

    $n = array();
    $n[$date] = array();
    $n[$date][0] = "";
    $n[$date][1] = "";

    #and then

    $n["10/01/09"][0] = "10";
    $n["10/01/09"][1] = "50";
    $n["11/02/09"][1] = "70";
    $n["01/05/09"][0] = "90";

---EDIT

you can see this functions in "array_merge" , "array_combine", ... but in this instance, this can be resolved

Comments

1

Here's my solution:

$a[0][0] = '11/15/08';
$a[0][1] = '50';
$a[1][0] = '11/20/08';
$a[1][1] = '75';
$a[2][0] = '01/04/09';
$a[2][1] = '23';
$a[3][0] = '01/09/09';
$a[3][1] = '92';

$b[0][0] = '01/04/09';
$b[0][1] = '30';
$b[1][0] = '01/05/09';
$b[1][1] = '54';
$b[2][0] = '01/08/09';
$b[2][1] = '89';
$b[3][0] = '01/09/09';
$b[3][1] = '62';

function dates($arr, &$result)
{
    foreach ($arr as $dates)
    {
        $result[$dates[0]][] = $dates[1];
    }
}

$result = array();
dates($a, $result);
dates($b, $result);

$dates = array();
$i = 0;
foreach ($result as $date => $res)
{
    $dates[$i][0] = $date;
    $dates[$i][1] = $res[0];
    if (count($res) == 1)
    {
        $dates[$i][2] = 0;
    } else {
        $dates[$i][2] = $res[1];
    }
    $i++;
}
print_r($dates);

Output:

Array
(
    [0] => Array
        (
            [0] => 11/15/08
            [1] => 50
            [2] => 0
        )

    [1] => Array
        (
            [0] => 11/20/08
            [1] => 75
            [2] => 0
        )

    [2] => Array
        (
            [0] => 01/04/09
            [1] => 23
            [2] => 30
        )

    [3] => Array
        (
            [0] => 01/09/09
            [1] => 92
            [2] => 62
        )

    [4] => Array
        (
            [0] => 01/05/09
            [1] => 54
            [2] => 0
        )

    [5] => Array
        (
            [0] => 01/08/09
            [1] => 89
            [2] => 0
        )

)

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.