Q: Merge 1d array into 2d array where there's a match I have an array that generates a timestamps for the last 7 days and I have a 2d array that gives a value to certain days within the last 7 days. I want to use the first array to fill in the days where no value exists under each key.
$hotel_data:
Array
(
    [49] => Array
        (
            [1365202800] => 2
        )

    [48] => Array
        (
        )

    [50] => Array
        (
            [1364943600] => 4
            [1365375600] => 5
        )
    )

$d:
Array
(
    [1365375600] => 0
    [1365289200] => 0
    [1365202800] => 0
    [1365116400] => 0
    [1365030000] => 0
    [1364943600] => 0
    [1364857200] => 0
    [1364770800] => 0
    [1364688000] => 0
    [1364601600] => 0
)

Here's the code I'm trying to use to merge the two arrays:
foreach($hotel_data as $key1=>$value1) {
    foreach($hotel_data[$key1] as $datekey=>$ratingval) {
        foreach($d as $key2=>$value2)
        {
            if($datekey !== $key2) {
                $hotel_data[$key1][$key2] = 0;
            }
        }   
            //echo $datekey.'<br/>';
    }
}   

And this is the result:
Array
(
    [49] => Array
        (
            [1365202800] => 2
            [1365375600] => 0
            [1365289200] => 0
            [1365116400] => 0
            [1365030000] => 0
            [1364943600] => 0
            [1364857200] => 0
            [1364770800] => 0
            [1364688000] => 0
            [1364601600] => 0
        )

    [48] => Array
        (
        )

    [50] => Array
        (
            [1364943600] => 0
            [1365375600] => 0
            [1365289200] => 0
            [1365202800] => 0
            [1365116400] => 0
            [1365030000] => 0
            [1364857200] => 0
            [1364770800] => 0
            [1364688000] => 0
            [1364601600] => 0
        )

)

But as you can see, for some reason the value of [49] remains but the two values that were in [50] have been replaced with 0. 

A: The reason is that you have 2 values that are set for 50.
When $datekey is the key for the first value, you set all others to 0, including the second value.
When $datekey is the key for the second value, you set all others to 0, including the first value.
I think it should work if you check $hotel_data[$key1][$key2] before setting it:
if (!isset($hotel_data[$key1][$key2]) { $hotel_data[$key1][$key2]=0;}
Using that you can actually remove your if.
