Solved

PHP Get Column Totals for data in a Two Dimensional Array

Posted on 2015-01-15
4
184 Views
Last Modified: 2015-01-16
I'm trying to total the columns in a two-dimensional array without much success.

Array look like:

object(stdClass)[685]
      public 'fusWeek' => string '45' (length=2)
      public 'Avg2012' => string '7.71' (length=4)
      public 'Avg2013' => string '7.84' (length=4)
      public 'Avg2014' => null
      public 'Avg2015' => null

I'm using the following code to do the calculation but just keep getting zero as the total

  $column = array_column($ary_table_data, 'Avg2012');
  $totals = array_sum($column);

Open in new window


I know that the data in each column is a number but is held as a string, but was hoping PHP was clever enough to translate this.

Any other ideas or thoughts on how I can get this total to work out correctly.
0
Comment
Question by:Sean Clarke
  • 2
4 Comments
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 40551583
I think you will need to convert the stings to float usinf floatval()

See http://php.net/manual/en/function.floatval.php

once you have the data converted, array_sum() should work

Cd&
0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 40552451
PHP can be helpful here if you "ask nicely" :-)

<?php // demo/temp_sean_clarke.php
error_reporting(E_ALL);

/** 
 * SEE: http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28597329.html
 *
 * object(stdClass)[685]
 *      public 'fusWeek' => string '45' (length=2)
 *      public 'Avg2012' => string '7.71' (length=4)
 *      public 'Avg2013' => string '7.84' (length=4)
 *      public 'Avg2014' => null
 *      public 'Avg2015' => null
 */

// TEST DATA FROM THE POST AT E-E 
$obj = new StdClass;
$obj->fusWeek ='45';
$obj->Avg2012 ='7.71';
$obj->Avg2013 ='7.84';
$obj->Avg2014 = null;
$obj->Avg2015 = null;

// DOES THIS TEST DATA LOOK LIKE THE EXAMPLE?  YES.
var_dump($obj);

// MAKE IT INTO AN ARRAY SO WE CAN USE ARRAY_SUM()
$arr = (array)$obj;
$sum = array_sum($arr);
var_dump($sum);

Open in new window

0
 
LVL 1

Author Closing Comment

by:Sean Clarke
ID: 40553127
Perfect thanks.
It was casting the stdClass to an array that sorted it out
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 40553225
Thanks for the points.  PHP has some object iterators that can help, too.  But the shortest path is just recasting the data type.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question