Passing array of Variables by Reference

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    Passing array of Variables by Reference

    I thought this would work
    Code:
    calling function
    {.............
    $cols = array($orders,$net,$charges,$margin);
    $tots = array(&$totorders,&$totnet,&$totcharges,&$totmargin);
    addTotals($cols,$tots);
    .............
    }
    
    function addTotals(&$columns,&$totals)
    {
    	$size = count($columns);
    	for($c=0;$c<$size;$c++)
    	{
    		$totals[$c] += $columns[$c];
    		$columns[$c] = 0;
    	}
    }
    What I want to happen is the variables $totorders,$tot net,$totcharges ,$totmargin are changed in the CALLING function.
    But the first one only $totorders gets added.
    The others $totnet,$totcha rges,$totmargin do not.

    How should I be doing this?
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    Wait a minute it may be working but the output is wrong.
    Better test some more.

    Comment

    Working...