
November 22nd, 2005
05:21 PM
can't touch this
Status: Offline!
count array values
how would I go about counting values of of an array like this;
name|20.00|age
name|40.00|age
name|50.00|age
and so on...
how would I add up all of the values of the second columns? e.g. in this case to get 110.00
any help would be appreciated 

November 22nd, 2005
05:55 PM
Neverside Newbie
Status: Offline!
use preg_split("|",$arrayname) first
then count($arrayname)

November 22nd, 2005
06:04 PM
Neversidian
Status: Offline!
since you will be looping through the file anyway, you can put this in your loop
<?php
//$str = 'name|20.00|age';
$str = explode('|', $str);
$sum += $str[1];
?>
also look at http://us3.php.net/manual/en/function.array-sum.php
___________________
-Developer
-Forum Leader
-NeverNET

November 22nd, 2005
06:30 PM
can't touch this
Status: Offline!
thanks, i was looking into array_sum but I couldn't get anything out of it

November 23rd, 2005
02:20 AM
Neversidian
Status: Offline!
to use array_sum you'd do something like
<?php
$lines = file('yourfile.txt');
$data = array();
foreach($lines as $key=>$line)
{
list($data['names'][$key], $data['count'][$key], $data['age'][$key]) = explode('|', $line);
}
echo array_sum($data['count']);
?>
___________________
Neverside Development Director
PHP Snippets
BigToach.com - IT WORKS, TOACHY!