0

I am trying to sum values inside a key while keeping the key name so i could print out a list of values...

this is the array i have:

Array ( 
    [Israel] => Array ( [0] => 3823 [1] => 1358 [2] => 496 [3] => 198 [4] => 134 [5] => 129 [6] => 107 [7] => 93 [8] => 91 [9] => 70 [10] => 60 [11] => 59 [12] => 54 [13] => 50 [15] => 35 [16] => 33 [17] => 33 [18] => 31 [19] => 28 [20] => 28 [24] => 19 [25] => 19 [27] => 18 [28] => 18 [29] => 15 [30] => 15 [31] => 14 [32] => 13 [33] => 13 [36] => 11 [37] => 10 [38] => 10 [39] => 10 [42] => 9 [45] => 7 [46] => 7 [51] => 6 [71] => 4 [72] => 4 [73] => 4 ) 
    [Palestine] => Array ( [14] => 48 ) 
    [Venezuela] => Array ( [21] => 21 [50] => 7 [64] => 5 [95] => 3 ) 
    [(not set)] => Array ( [22] => 20 ) 
    [United Kingdom] => Array ( [23] => 20 ) 
    [United States] => Array ( [26] => 19 [35] => 12 [40] => 10 [43] => 8 [47] => 7 [48] => 7 [49] => 7 [53] => 6 [54] => 6 [55] => 6 [56] => 6 [63] => 5 [76] => 4 [77] => 4 [90] => 3 [91] => 3 [92] => 3 [93] => 3 [94] => 3 ) 
    [Ecuador] => Array ( [34] => 12 [44] => 7 [83] => 3 ) 
    [Australia] => Array ( [41] => 9 [57] => 5 [97] => 2 ) 
    [Peru] => Array ( [52] => 6 ) 
    [China] => Array ( [58] => 5 ) 
    [El Salvador] => Array ( [59] => 5 ) 
    [Germany] => Array ( [60] => 5 [69] => 4 [85] => 3 [86] => 3 ) 
    [Paraguay] => Array ( [61] => 5 ) 
    [Poland] => Array ( [62] => 5 ) 
    [Armenia] => Array ( [65] => 4 ) 
    [Bolivia] => Array ( [66] => 4 [80] => 3 [81] => 3 [82] => 3 ) 
    [Canada] => Array ( [67] => 4 ) 
    [France] => Array ( [68] => 4 ) 
    [Guatemala] => Array ( [70] => 4 ) 
    [Nicaragua] => Array ( [74] => 4 ) 
    [Spain] => Array ( [75] => 4 [89] => 3 ) 
    [Uruguay] => Array ( [78] => 4 ) 
    [Argentina] => Array ( [79] => 3 ) 
    [Egypt] => Array ( [84] => 3 ) 
    [Italy] => Array ( [87] => 3 ) 
    [Nepal] => Array ( [88] => 3 ) 
    [Algeria] => Array ( [96] => 2 ) 
    [Belarus] => Array ( [98] => 2 ) 
    [Bulgaria] => Array ( [99] => 2 ) 
) 

I am looking for a result that is similer to this:

isael : 3400 Palestine: 48 Venezuela: 36

i tried using this but it doesent suit and i dont know how to convert it

ETC...

1
  • can you post the proper array.. Commented May 26, 2013 at 8:54

2 Answers 2

2

Use array_sum():

$output = array();
foreach ($array as $k => $a) {
  $output[$k] = array_sum($a);
}
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for your help... that answer works so.. +1 for the effort ;)
1

How about :

foreach ($myArray as $k=>$subArray) {   
    $sumArray[$k] = 0;   
    foreach ($subArray as $id=>$value) {
       $sumArray[$k]+=$value;   
    }
}

as $k represents your country, you sum all values of it's subarray and store it in a map which has a key with the name stored in $k.

1 Comment

:) so simple but i couldnt get my brain to wrap around it! thanks a lot mate. it works!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.