Solved

PHP Get Column Totals for data in a Two Dimensional Array

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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

632 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