1

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.

2
  • Please tag it as "php" if it is the language you're using. Commented Apr 8, 2013 at 16:44
  • i think the problem is because the same date appears in all arrays? Commented Apr 8, 2013 at 18:02

1 Answer 1

1

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.

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

1 Comment

Fantastic, it worked! I didn't thin to check if it was empty. Thank you.

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.