Banner

Sponsor

Login


Welcome Back!
Guest
Guest

Register

Lost your password?

83 users online



count array values

count array values

Currently viewing this thread: 1 (0 members and 1 guests)


webstar

webstar

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 Smile

reregistered

reregistered

Neverside Newbie
Status: Offline!

use preg_split("|",$arrayname) first
then count($arrayname)

aonic

aonic

Neversidian
Status: Offline!

since you will be looping through the file anyway, you can put this in your loop

PHP:

<?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

webstar

webstar

can't touch this
Status: Offline!

thanks, i was looking into array_sum but I couldn't get anything out of it

BigToach

BigToach

Neversidian
Status: Offline!

to use array_sum you'd do something like

PHP:

<?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!

Quick Jump:

Main Navigation


Site & Graphic Design by Aeon Tan
Developed by Jeremie Pelletier & Scott Roach


NeverAPI generated this page in 0.0078 seconds.