1

I searched for few in-built methods and tried few logics from here and there but none can make me satisfied. I have created my own logic but is limited to a certain dimension of the array. I have managed to solve my problem for the project but as I said I am not satisfied.

Consider the array below:

 
$data = array
 (
    '0' => array
        (
            '0' => array
                (
                    'make_ready' => '',
                    'ready_time' => '0.55',
                    'rate' => '46',
                    'no_of_run' => '',
                    'fixed_cost' => '',
                    'variable_cost' => '25.3',
                    '0' => array(
                            'kg' => 2.66,
                            'rate' => 11.4,
                            'fixed_cost' => '',
                            'variable_cost' => 30.32,
                            '0' => array(
                                    'kg' => 2.66,
                                    'rate' => 11.4,
                                    'fixed_cost' => '',
                                    'variable_cost' => 30.32,
                                   ),
                        ),
                ),

        ),

    '1' => array
        (
            '0' => array
                (
                    'make_ready' => '1',
                    'ready_time' => '1.16',
                    'rate' => '36.47',
                    'no_of_run' => '',
                    'fixed_cost' => '36.47',
                    'variable_cost' => '42.31',
                ),

        ),

    '2' => array
        (
            'make_ready' => '2',
            'ready_time' => '0.29',
            'rate' => '360',
            'no_of_run' => '',
            'fixed_cost' => '720',
            'variable_cost' => '104.4',
        ),

    'size' => '1000 X 1200 X 1190',
    'up' => '3 X 4 / 17',
    'unit' => '4',
    'rate' => 16.32,
    'fixed_cost' => '',
    'variable_cost' => 65.28,
);
 

Problem:

I want to sum up all the values of array element where key is 'variable_cost', no matter how deep (in terms of dimension) is the array. Basically like a loop that can scan all the elements and do 'sum' if key matches.

I would like someone to help/suggest any logic where the calculations can be done till nth dimension.

For example like the movie Inception, in which they can go

dream->with in a dream->to nth number of dream.

and come out with the sum of 'variable_cost'. Hope you guys can understand the question here.

Thank you.

1
  • How are you generating this array? There is no uniformity anywhere in it which makes this problem harder for you. Commented Jan 12, 2016 at 3:49

1 Answer 1

1

You will need to use a recursive function to sum all the variable_cost values:

function sum_variable_cost( $data, $total=0)  {
    foreach ($data as $key => $item) {
        if (is_array($item)) {
            $total = sum_variable_cost($item, $total);
        } else if ($key=='variable_cost') {
            //echo $key . " " . $item . "\n";
            $total += $item;
        }
    }
    return $total;
}

echo sum_variable_cost( $data );

Basically it loops through the array and sees if there are other arrays and only adds values that contain the key variable_cost.

If you uncomment the echo line it will show this output:

variable_cost 25.3
variable_cost 30.32
variable_cost 30.32
variable_cost 42.31
variable_cost 104.4
variable_cost 65.28
297.93
Sign up to request clarification or add additional context in comments.

3 Comments

thank you, that was so easy, I think I overlooked at the problem too much. Thank you.
Hi, I had remove the tick (apologies), I found a bug in the logic, the final total is not the correct answer. For eg: 25.3 + 30.32 + 30.32 + 42.31 + 104.4 + 65.28 = 297.93. From the logic the final answer is incorrect. Thank you
I've updated my answer, it needed to be $total = sum_variable_cost($item, $total); .. feel free to re-add the tick.

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.