Solved

PHP Get Column Totals for data in a Two Dimensional Array

Posted on 2015-01-15
4
177 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 108

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 108

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now