subhomoy Posted July 16, 2014 Share Posted July 16, 2014 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... Quote Link to comment https://forums.phpfreaks.com/topic/289936-summation-of-array/ Share on other sites More sharing options...
requinix Posted July 16, 2014 Share Posted July 16, 2014 (edited) Why are the 16/07/14 values being merged into 17/07/14's? Why choose 17's date as the final key and not 16's? Edited July 16, 2014 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/289936-summation-of-array/#findComment-1485461 Share on other sites More sharing options...
subhomoy Posted July 17, 2014 Author Share Posted July 17, 2014 (edited) sorry for the wrong info given... I want the output as this.... Array ( [Vu] => 25 [No] => 52 [Al] => 50 [LG] => 70 ) Now i think its ok.... Edited July 17, 2014 by subhomoy Quote Link to comment https://forums.phpfreaks.com/topic/289936-summation-of-array/#findComment-1485515 Share on other sites More sharing options...
requinix Posted July 17, 2014 Share Posted July 17, 2014 (edited) You're executing a query to get those numbers, right? What's the query? I bet we could change it a bit to return exactly the results you want. Edited July 17, 2014 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/289936-summation-of-array/#findComment-1485516 Share on other sites More sharing options...
kicken Posted July 17, 2014 Share Posted July 17, 2014 ... 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 Quote Link to comment https://forums.phpfreaks.com/topic/289936-summation-of-array/#findComment-1485517 Share on other sites More sharing options...
subhomoy Posted July 17, 2014 Author Share Posted July 17, 2014 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,,,,, Quote Link to comment https://forums.phpfreaks.com/topic/289936-summation-of-array/#findComment-1485520 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.