0

I have this Array

  Array
(
    [2014-08-14] => Array
        (
            [18:00:00] => Array
                (
                    [6] => Array
                        (
                            [price] => 15.36
                            [avail_clean] => 5
                            [avail_noclean] => 6
                        )

                    [7] => Array
                        (
                            [price] => 17.35
                            [avail_clean] => 2
                            [avail_noclean] => 3
                        )

                )

            [19:00:00] => Array
                (
                    [6] => Array
                        (
                            [price] => 15.36
                            [avail_clean] => 5
                            [avail_noclean] => 6
                        )

                    [7] => Array
                        (
                            [price] => 17.35
                            [avail_clean] => 2
                            [avail_noclean] => 3
                        )

                )

        )

)

How can I get the following for 6 & 7 seperately: - Sum of price - max of avail_clean - max of avail_noclean

I got that far for the price:

foreach ($bookable as $date=>$key) {
    foreach ($key as $time=>$key2) {
        foreach($key2 as $room=>$key3){
            foreach($key3 as $price=>$key4){
                if($price == "price"){
                    if(isset($sumRoom[$room]['total'])){
                        $sumRoom[$room]['total'] += $key4;
                    }else{
                        $sumRoom[$room]['total'] = $key4;
                    }
                }
            }
        }

    }
}

Gives me this

Array(
[6] => Array
    (
        [total] => 30,72
    )

[7] => Array
    (
        [total] => 34,7
    )

)

But what about the max(), where should I put that?

4
  • you need to clarify your goal. what do you mean by max? you want to get the highest avail_clean and avail_noclean number?, you already got you sum, should be the same path, if not initialized assign it, then under the loop just check if the current number is higher, if yes then reassign Commented Aug 14, 2014 at 6:22
  • I'd like to get the max of avail_clean and the max of avail_noclean. Isn't the "loop compare" method the "dirty" one? Shouldn't I use max() for the more elegant way? Commented Aug 14, 2014 at 6:31
  • if that's what you want, then just before the last inner loop map all the avail_clean and avail_clean then use max() Commented Aug 14, 2014 at 6:37
  • Have you found answer? If not, you can check my answer once!! Commented Aug 14, 2014 at 7:27

1 Answer 1

1
foreach ($bookable as $date=>$times) {
    foreach ($times as $time=>$rooms) {
        foreach($rooms as $room=>$options){ $sumRoom[$room]['total'] = 0;
            foreach($options as $option=>$value){

                if($option == "price"){
                    $sumRoom[$room]['total'] += $value;
                }
                if($option == "avail_clean"){
                    $avail_clean[$room][] = $value;
                }
                if($option == "avail_noclean"){
                    $avail_noclean[$room][] = $value;
                }

            }
            $sumRoom[$room]['avail_clean_max'] = max($avail_clean[$room]);
            $sumRoom[$room]['avail_noclean'] = max($avail_noclean[$room]);
        }
    }
}

I've edited my answer.. I've merged all in one array i.e. $sumRoom

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

1 Comment

tried to use before edit and edited myself to the same outcome (more or less). Works like a charm now! Thanks

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.