conbask
conbask

Reputation: 10071

Combining arrays with common values

I have an array of dates that looks like this:

Array
(
    [0] => '2014-01-01'
    [1] => '2014-01-02'
    [2] => '2014-01-03'
    [3] => '2014-01-04'
    [4] => '2014-01-05'
    [5] => '2014-01-06'
    [6] => '2014-01-07'
)

and I have another array of dates and counts that looks like this:

[All] => Array
    (
        [0] => Array
            (
                [count] => 2
                [date] => 2014-01-06
            )

        [1] => Array
            (
                [count] => 1
                [date] => 2014-01-03
            )

        [2] => Array
            (
                [count] => 43
                [date] => 2013-12-11
            )

        [3] => Array
            (
                [count] => 103
                [date] => 2013-12-10
            )

        [4] => Array
            (
                [count] => 128
                [date] => 2013-12-09
            )

        [5] => Array
            (
                [count] => 75
                [date] => 2013-12-08
            )

        [6] => Array
            (
                [count] => 107
                [date] => 2013-12-07
            )

I want to make a new associative array where all the keys are the dates from the first array above and all of the values are either the count matched up with the corresponding date or "0".

So for instance, the new array would look like this:

Array
(
    [2014-01-01] => 0
    [2014-01-02] => 0
    [2014-01-03] => 1
    [2014-01-04] => 0
    [2014-01-05] => 0
    [2014-01-06] => 2
    [2014-01-07] => 0
)

Does that make sense? Please feel free to ask any questions you may have. Thank you!

Upvotes: 0

Views: 57

Answers (4)

jek
jek

Reputation: 2396

That's a 2-liner:

// use dates as index and set everything to 0
$result = array_fill_keys($x, 0));
// copy over existing counts
array_walk($all, function($v) use (&$result) { if (array_key_exists($v['date'], $result)) { $result[$v['date']] = $v['count'];}});

Upvotes: 0

Eisa Adil
Eisa Adil

Reputation: 1733

$new = array();

foreach($all as $row)
{
       $new[$row['date']] = $row['count'];
}

array_merge ($new, $old);

Here $all is the array with the date and count indices. $old is the exisisting array.

Upvotes: 0

u_mulder
u_mulder

Reputation: 54831

$result = array();
foreach($secondArray as $s){
    if(in_array($s['date'], $firstArray) {
        unset($firstArray[$s['date']]);
        $result[$s['date']] = $s['count'];
    }        
}

// if items left in first array that are not found within foreach:
if (!empty($firstArray))
    $result = array_merge($result, array_fill_keys($firstArray, 0));

// sort by key so dates go ascending
ksort($result);

Upvotes: 0

Ahmad
Ahmad

Reputation: 5760

Try this code:

$result = array();
foreach($firstArray as $f){
    foreach($secondArray as $s){
        if($s['date'] == $f) $result[$f] = $s['count'];
    }
    if(!array_key_exists($f, $result)) $result[$f] = 0;
}

Upvotes: 1

Related Questions