
function add2array(&$array,$index,$value)
//++
// add2array
// construct array ($ARRAY) where $value(s) are summed for duplicate $index
// eg. given a series:
// array index1 1
// array index2 2
// array index1 3
// will produce:
// array["index1"] = 4
// array["index2"] = 2
//
// Inputs: Array name (by reference), Index name, Value
//
// Outputs: Array, with individual index values summed.
//
// Errors:
//--
{
// if index does not exist, add it and initalise with value
// Otherwise, add $value to value at index.
if (array_key_exists($index,$array))
{
$array["$index"] = $value;
exit();
}
$array["$index"] = $array["$index"]+$value;
exit();
}
This is the calling code:
<?php
include ('/APACHE$ROOT/PHP/SCRIPTS/BNK_LIB.PHP');
$idx = array(one,two,three,three,two,one);
$val = array(1,2,3,3,2,1);
$outp= array();
print_r ($idx);
for ($i=0; $i<5; $i++)
{
add2array(&$outp,$idx[$i],$val[$i]);
}
print_r ($outp);
?>
The problem is that I don't get $outp with anything in it. Can someone please point out my stupidity?
Many thanks
Paul