1

I have a large array with some data. I was trying to make only one function to return the sum of each value from the beginning of the month until the end of the month for each group.

I have created a function that returns the total of a "key", but I have to repeat myself all the time for each key. Is it possible to make multiple return values with only one function like putting them in an array?

// I'm using this function for returning the value
function kontratat_tot_grupi($date, $grup_name, $value) {
    $tot_grupi = $date[$grup_name][$value];
    return $tot_grupi;
}

// And here I'm using the function on grup_a in a 'while' loop
$tot_grup_a += kontratat_tot_grupi($date, "grup_a", "luce");

// This is a piece from my array
Array
(
    [2016-05-02] => Array
        (
            [grup_a] => Array
                (
                    [luce] => 4
                    [ctr_ok] => 3
                    [ctr_tot] => 7
                    [ctr_ko] => 4
                    [gas] => 3
                    [ore] => 30.5
                )

            [grup_b] => Array
                (
                    [luce] => 3
                    [ctr_ko] => 4
                    [ctr_tot] => 6
                    [gas] => 3
                    [ctr_ok] => 2
                    [ore] => 47
                )

            [grup_c] => Array
                (
                    [luce] => 6
                    [ctr_ko] => 1
                    [ctr_tot] => 8
                    [ctr_gia_cliente] => 1
                    [ctr_ok] => 6
                    [gas] => 2
                    [ore] => 24
                )

            [grup_d] => Array
                (
                    [luce] => 4
                    [ctr_ok] => 4
                    [ctr_tot] => 8
                    [gas] => 4
                    [ctr_ko] => 4
                    [ore] => 30
                )

            [grup_e] => Array
                (
                    [luce] => 9
                    [ctr_ko] => 11
                    [ctr_tot] => 17
                    [gas] => 8
                    [ctr_ok] => 6
                    [ore] => 35
                )

            [grup_f] => Array
                (
                    [luce] => 1
                    [ctr_ok] => 2
                    [ctr_tot] => 2
                    [gas] => 1
                    [ore] => 36
                )

            [grup_g] => Array
                (
                    [luce] => 5
                    [ctr_ko] => 1
                    [ctr_tot] => 7
                    [ctr_ok] => 6
                    [gas] => 2
                    [ore] => 22
                )

        )

    [2016-05-03] => Array
        (
            [grup_a] => Array
                (
                    [luce] => 6
                    [ctr_ok] => 6
                    [ctr_tot] => 10
                    [gas] => 4
                    [ctr_ko] => 4
                    [ore] => 33.5
                )

            [grup_b] => Array
                (
                    [luce] => 6
                    [ctr_ok] => 4
                    [ctr_tot] => 8
                    [ctr_ko] => 2
                    [gas] => 2
                    [ctr_att_green] => 2
                    [ore] => 36
                )

            [grup_c] => Array
                (
                    [luce] => 6
                    [ctr_ok] => 4
                    [ctr_tot] => 9
                    [gas] => 3
                    [ctr_ko] => 5
                    [ore] => 36
                )

            [grup_d] => Array
                (
                    [luce] => 5
                    [ctr_ko] => 2
                    [ctr_tot] => 10
                    [gas] => 5
                    [ctr_ok] => 8
                    [ore] => 42
                )

            [grup_e] => Array
                (
                    [gas] => 2
                    [ctr_ok] => 3
                    [ctr_tot] => 3
                    [luce] => 1
                    [ore] => 23
                )

            [grup_f] => Array
                (
                    [luce] => 1
                    [ctr_ko] => 2
                    [ctr_tot] => 2
                    [gas] => 1
                    [ore] => 36
                )

            [grup_g] => Array
                (
                    [luce] => 2
                    [ctr_ok] => 1
                    [ctr_tot] => 3
                    [ctr_gia_cliente] => 2
                    [gas] => 1
                    [ore] => 27.3
                )

        )

    [2016-05-04] => Array
        (
            [grup_a] => Array
                (
                    [luce] => 6
                    [ctr_ko] => 1
                    [ctr_tot] => 11
                    [ctr_ok] => 10
                    [gas] => 5
                    [ore] => 32
                )

            [grup_b] => Array
                (
                    [luce] => 5
                    [ctr_ok] => 10
                    [ctr_tot] => 11
                    [gas] => 6
                    [ctr_ko] => 1
                    [ore] => 33.4
                )

            [grup_c] => Array
                (
                    [luce] => 8
                    [ctr_ok] => 15
                    [ctr_tot] => 15
                    [gas] => 7
                    [ore] => 36
                )

            [grup_d] => Array
                (
                    [luce] => 5
                    [ctr_ok] => 5
                    [ctr_tot] => 8
                    [gas] => 3
                    [ctr_att_egeo] => 1
                    [ctr_ko] => 2
                    [ore] => 47
                )

            [grup_e] => Array
                (
                    [luce] => 4
                    [ctr_ok] => 5
                    [ctr_tot] => 5
                    [gas] => 1
                    [ore] => 36
                )

            [grup_f] => Array
                (
                    [luce] => 3
                    [ctr_ko] => 5
                    [ctr_tot] => 6
                    [gas] => 3
                    [ctr_ok] => 1
                    [ore] => 33
                )

            [grup_g] => Array
                (
                    [luce] => 8
                    [ctr_ok] => 12
                    [ctr_tot] => 12
                    [gas] => 4
                    [ore] => 36
                )

        )
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rafael Shkembi
  • 786
  • 6
  • 16
  • No, you can't make multiple result on on function. – user3502626 May 28 '16 at 22:20
  • Yes, you can return an array from the function.... you can only return one data value, but that value can be an array – Mark Baker May 28 '16 at 22:23
  • 1
    An alternative might be to use the function as a generator, so that you can iterate over it, returning a different element from the array each time: `function kontratat_tot_grupi($date,$grup_name){ foreach ( $date[$grup_name] as $key = $value); yield $key => $value; } }` – Mark Baker May 28 '16 at 22:23
  • I don't really understand your code but, your array example seems ok. And if you want, you can call a function multiple times in for or a while. There you can take the function returned value and do what you want. – user3502626 May 28 '16 at 22:26
  • Thank you very much @MarkBaker for your help i will try your suggestion :) – Rafael Shkembi May 28 '16 at 22:35
  • 1
    Possible duplicate of [Multiple returns from function](http://stackoverflow.com/questions/3451906/multiple-returns-from-function) – miken32 May 29 '16 at 02:28

0 Answers0