Help with PHP arrays please

Discussions around the use of PHP on VMS
Post Reply
Message
Author
User avatar
beaudoin_p
$ HELP
Posts: 13
Joined: Fri Oct 21, 2005 10:31 am
Location: London UK

Help with PHP arrays please

#1 Post by beaudoin_p »

I' m new to php and would li :oops: ke some help. The function here explains what I want it to do:
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

User avatar
Peter Weaver
$ HELP
Posts: 14
Joined: Fri Sep 16, 2005 12:04 am
Contact:

#2 Post by Peter Weaver »

I think this is what you want. I changed your IF statement around and took out the exit() statements since that exited the whole thing, not just the function. I also increased your loop to get all the elements. I changed the names in the funtion, but I do not think that matters. I forget what else I did because I had 41 versions of test_array.php by the time I was done. Hope that helps.

$ type test_array.php
<?php
function add2array (&$array2,$index2,$value2)
//++
// 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.
print "testing if index $index2 exists in array\n";
if (array_key_exists($index2,$array2))
{
print "Key $index2 already in array adding $value2 to $array[$index2] >>";
$array2[$index2] = $array2[$index2]+$value2;
} else {
$array2[$index2] = $value2;
print "*****Initialized $index2 to $value2 >>";
}
print "index $index2 is now $array2[$index2]\n";
}

$idx = array(one,two,three,three,two,one);
$val = array(1,2,3,3,2,1);
$outp= array();
print_r ($idx);
print_r ($val);
print_r ($outp);
print "Starting loop \n";
for ($i=0; $i<6; $i++)
{
add2array($outp,$idx[$i],$val[$i]);
}
print_r ($outp);
?>
$ php test_array.php
Content-type: text/html
X-Powered-By: PHP/4.3.2

Array
(
[0] => one
[1] => two
[2] => three
[3] => three
[4] => two
[5] => one
)
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 3
[4] => 2
[5] => 1
)
Array
(
)
Starting loop
testing if index one exists in array
*****Initialized one to 1 >>index one is now 1
testing if index two exists in array
*****Initialized two to 2 >>index two is now 2
testing if index three exists in array
*****Initialized three to 3 >>index three is now 3
testing if index three exists in array
Key three already in array adding 3 to >>index three is now 6
testing if index two exists in array
Key two already in array adding 2 to >>index two is now 4
testing if index one exists in array
Key one already in array adding 1 to >>index one is now 2
Array
(
[one] => 2
[two] => 4
[three] => 6
)

User avatar
beaudoin_p
$ HELP
Posts: 13
Joined: Fri Oct 21, 2005 10:31 am
Location: London UK

#3 Post by beaudoin_p »

Peter,

I am in your debt. I will have to study the differences to fully understand where I went wrong but in any case, the code you supplied does the trick

Many thanks

Paul

Post Reply