php - Counting an eliminating duplicate associative values in an array -
I have the following issue, I have an array with $ data name
of this array Within me is something like
[6] => Array ([code] => 642 [total] => 1708) [7] = & gt; Array ([code] => 642 [total] => 53) [8] = & gt; Array ([code] => 642 [total] => 1421)
The code element is the same, now what I want to do is get the same code value together, and the sum Add together I tried to do it in the foreshop loop, but it does not seem to work.
I do something like this
$ old_lc = null; $ Old_lcv = 0; $ Count = 0; $ Dd = null; Foreign currency ($ $$ data) {if ($ d ['code'] == $ old_lc) {$ d ['total'] = $ d ['total'] + $ old_lcv; $ Count--; $ Dd [$ count] ['code'] = $ d ['code']; $ Dd [$ count] ['Total'] = $ d ['total']; } And {$ dd [$ count] ['code'] = $ d ['code']; $ Dd [$ count] ['Total'] = $ d ['total']; $ Count ++; } $ Old_lc = $ d ['Code']; $ Old_lcv = $ d ['total']; } $ Data = $ dd;
But this does not seem to work. Besides, I need a $ data array to keep the key, and should remain in the same format
This code translates the above array into an array of code => total.
$ out = array (); Foreign currency ($$ k = $$ as data $> $ v) {$ out [$ v ['code']] + = $ v ['total']; }
It is worth noting that on certain settings it will generate a warning about the undefined index. If this disturbs you, you can use this alternate version:
$ out = array (); ($ V ['code'], $ out) {$ out [$ v ['code']] + = $ v [$ ($ c = & $; 'Total']; } And {$ out [$ v ['code']] = $ v ['code']; }}
It changes it back in something like the original, if it is what you want:
$ output = array (); Forex currency ($$$$ = $ gt; = $ $ as $) [$ output [==] ('code' = & gt; $ code, 'total' = & gt; $ total);
Note: The original key of $ data has not been maintained but it was not said that it was a requirement if If so, then it should be specified how to reassign many elements with the same code.
Comments
Post a Comment