Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PHP Get Column Totals for data in a Two Dimensional Array

Posted on 2015-01-15
4
Medium Priority
?
193 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 111

Accepted Solution

by:
Ray Paseur earned 2000 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 111

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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

688 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