array_walk_recursive, function using variable outside in PHP
<pre><code>   $total_materials_cost = 0;
   array_walk_recursive($materials, function($item, $key) {
        if (in_array($key, array('id'))) {

                    (....)

                    $total = $material_price * $material['amount'];

                    $total_materials_cost += $total;
                }
            }
        }
    });
echo $total_materials_cost;
</code></pre>

<p>For the above code I get error at line <code>$total_materials_cost += $total;</code> , says the variable is undefined - I believe this is because that I am inside a function? But how can I in any way bypass/ make a workaround for this, so it does add to the variable?</p>
