Jump to content

Summation of array


subhomoy

Recommended Posts

hello everybody

 

I have a database of many rows out of which i have a column of mobile impression Which i've kept by serializing an array.

The problem i'm facing is that i need to add the rows according to its key...

 

My code

 

The array structure is shown below

Array
(
    [16/07/14] => Array
        (
            [Vu] => 13
            [No] => 49
            [Al] => 45
            [LG] => 69
        )

Array
(
    [17/07/14] => Array
        (
            [Vu] => 12
            [No] => 3
            [Al] => 5
            [LG] => 1
        )
)

The above array can be a very big array.... and it is dynamically fetched from database using a while loop.

 

I want the output as

Array
(
    [17/07/14] => Array
        (
            [Vu] => 25
            [No] => 52
            [Al] => 50
            [LG] => 70
        )
)

The code i've used is

while($row = $q->fetch(PDO::FETCH_ASSOC))
 {
   $mob = unserialize($row['mobile_hits_received']);
   foreach ($mob as $value)
      {
               foreach($value as $key => $value1)
                       {
                          $final_mobile[$key] =  $value[$value]+0;
                       }
      }
}

Thank u in advance...

Link to comment
Share on other sites

... Which i've kept by serializing an array.

You're doing it wrong. Without knowing what data exactly you're trying to store and relate we can't say for sure what kind of table structure you should be using, but you should not be storing a serialized array. Instead you should be storing each of those numbers in the array as separate rows in the table. For example, your table would look like this:

create table mobile_hits_received (
   hitDate date
   , label char(2)
   , hitCount int
)
plus whatever other ID columns you need to relate the data if any.

 

Then you'd have separate rows in the table, eg:

+-------------+---------+------------+
|  hitDate    |  label  |  hitCount  |
+-------------+---------+------------+
|  2014-7-16  |  VU     |  13        |
|  2014-7-16  |  NO     |  49        |
|  2014-7-16  |  AL     |  45        |
|  2014-7-16  |  LG     |  69        |
|  2014-7-17  |  VU     |  12        |
|  2014-7-17  |  NO     |  3         |
|  2014-7-17  |  AL     |  5         |
|  2014-7-17  |  LG     |  1         |
+-------------+---------+------------+
Once you re-structure your application to store data properly you can get your sums with a simple select query:

SELECT
	label,
	SUM(hitCount)
FROM mobile_hits_received 
GROUP BY
	label
See it in action
Link to comment
Share on other sites

Thanks for the reply guyss but that is what i dont want...

 

Ok let me make it very clear...

 

I have an multidimentional array which stores mobile impression per day basis..

 

Thats why i used the format
 

Array
(
    [16/07/14] => Array
        (
            [Vu] => 13
            [No] => 49
            [Al] => 45
            [LG] => 69
        )

Array
(
    [17/07/14] => Array
        (
            [Vu] => 12
            [No] => 3
            [Al] => 5
            [LG] => 1
        )
)

Then i'm serializing the array and inserting into the database...

 

so

 

whenever i'm quering the array from the datatbase, first i need to unserialize it to make it an array and then produce the sum of that array...

 

therefore i need a while loop as there are many rows in the database which contain similar type of array...

 

I hope I've made it clear...

 

Thank u in advance,,,,,

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.