I am reading data from file and displaying array like below code :
if (($fp = fopen("test.txt", "r")) !== FALSE) {
$count = 0;
while(($row = fgetcsv($fp)) !== FALSE)
{
$row = explode("|",$row[0]);
foreach($row as &$el)
{
$el=trim($el);
}
$count++;
$tot = array_sum(array_column($row,2));
echo "<pre>";print_r($row);
if($count>3)
{
break;
}
echo "Coumt :".$tot;
}
echo "Coumt :".$tot;
fclose($fp);
}
Test.txt file data :
005-4410040 |BIRM| 0
005-4410040 |CHI |
450 005-4410040 |CIN | 144
I want total sum of 2nd index of the array it means 320 + 450 + 144
in seperate varriable.
How can i achieve this ? Already tried array_column but its not working.
Update What i have tried :
$sum = array_sum(array_column($row,$row['2']));