Serenitee Posted June 10, 2008 Share Posted June 10, 2008 I keep waiting for the lightbulb (sledgehammer) moment, and it hasn't occurred no matter how many tutorials I read - so once again turning here. I have this array: Array ( [Arms] => Array ( [97] => 2 [96] => 3 [98] => 2 [99] => 1 ) [Pants] => Array ( [97] => 1 [99] => 1 ) [Gloves] => Array ( [97] => 1 [96] => 1 [99] => 1 ) [boots] => Array ( [99] => 1 ) ) And would like to transform it into this: Array ( [Arms] => 8 [Pants] => 2 [Gloves] => 3 [boots] => 1 ) If my reading has garnered me any understanding, I am hoping to turn a multidimensional array into an associative (I'm still trying to learn what everything is called, so confirmation or correction would be grand), as well as add the values. I'm quite sure this is a very easy matter that I'm just not "getting" and would appreciate any assistance or pointers to clear tutorials. tia! -Ser Quote Link to comment Share on other sites More sharing options...
Orio Posted June 10, 2008 Share Posted June 10, 2008 <?php function sum_array ($array) { $result = array(); foreach($array as $key => $val) //Sum each sub-array and put it into $result { //Do the sum $sum = 0; foreach($val as $num) $sum += $num; //Enter the sum into $result $result[$key] = $sum; } return $result; } ?> And you were right about the terms Orio. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted June 10, 2008 Share Posted June 10, 2008 The only thing that defines an associative array is one in which the key has some meaning -- that is, it is is some way associated with the value. You would therefore wish to maintain this association throughout all array operations (sorting etc). This is as opposed to an array in which the keys have no specific meaning beyond being used to access the values. A multidimensional array is one in which one or more values of the array are arrays themsleves. So yeah, in essence, you were correct. But I thought i'd provide some clarification. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted June 10, 2008 Share Posted June 10, 2008 Here's a shorter solution: <?php $start = Array ( 'Arms' => Array ( 97 => 2, 96 => 3, 98 => 2, 99 => 1), 'Pants' => Array ( 97 => 1, 99 => 1), 'Gloves' => Array ( 97 => 1, 96 => 1, 99 => 1), 'Boots' => Array ( 99 => 1)); $end = array(); foreach ($start as $k => $v) $end[$k] = array_sum($v); print_r($end); ?> Ken Quote Link to comment Share on other sites More sharing options...
Serenitee Posted June 10, 2008 Author Share Posted June 10, 2008 Thank you both - it's nice to know the reading I have done has sunk in some The coding is not providing intended results, but I'm quite sure it's an edit I've done so will post the changes I made in hopes of a point to my newb error. $finCountArr = $QuaItem;//Copied actual array so I don't mess up other tasks //checked copy - all was well //echo '<pre>'; //echo print_r($finCountArr); //echo '</pre>'; function sum_array ($finCountArr) { $result = array(); foreach($finCountArr as $key => $val) //Sum each sub-array and put it into $result { //Do the sum $sum = 0; foreach($val as $num) $sum += $num; //Enter the sum into $result $result[$key] = $sum; } return $result; } All I've edited is "$array" to "$finCountArr" in both instances, providing its 'real' name, which seems logical - but perhaps that has created an issue. When I print_r($result) it returns: 1 tia, -Ser Edit: while I was posting, Ken added in some coding as well - which worked like a charm, thank you! I am still curious how I newbed up Orio's code though if any see what I may have killed it with. As usual, ya'all rock, thank you for taking the time Quote Link to comment Share on other sites More sharing options...
Orio Posted June 10, 2008 Share Posted June 10, 2008 I don't see you calling the function anywhere. But nevermind, as long as it works. Orio. Quote Link to comment Share on other sites More sharing options...
Serenitee Posted June 10, 2008 Author Share Posted June 10, 2008 LOL, I'm always happy with the "it works" part, thanks again Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted June 10, 2008 Share Posted June 10, 2008 I think you misread Orio -- you're code wasn't working, but only because the function was never called. Ken's code worked (it wasn't a function). You need to call the the function like so: function sum_array ($finCountArr) { $result = array(); foreach($finCountArr as $key => $val) //Sum each sub-array and put it into $result { //Do the sum $sum = 0; foreach($val as $num) $sum += $num; //Enter the sum into $result $result[$key] = $sum; } return $result; } sum_array($finCountArr); The reason why print_r() was echoing a 1 was just that -- you were echoing it, but it already does that for you in the function. You can either do this: echo '<pre>'; print_r($finCountArr); echo '</pre>'; Or, you can return the output of print_r by setting the second paramenter to true, and then echo it: echo '<pre>'.print_r($finCountArr,1).'</pre>'; Quote Link to comment Share on other sites More sharing options...
Serenitee Posted June 10, 2008 Author Share Posted June 10, 2008 Do I understand properly that I was missing the final "sum_array($finCountArr);" and that's how I broke it? If I do not understand properly.. I seem to be missing what you were drawing my attention to GingerRobot. Now I have a lovely new error, that when the coding is by itself works perfectly, but when put in the rest of my script is returning errors - still researching, but will likely be posting it later as all I'm finding is the actual error in my searches, not what might be causing it. That, however, is for a new post if I can not find the answer tia, -Ser Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted June 10, 2008 Share Posted June 10, 2008 Do I understand properly that I was missing the final "sum_array($finCountArr);" and that's how I broke it? That's correct. You must call a function, otherwise the code in it will not be executed. Quote Link to comment Share on other sites More sharing options...
Serenitee Posted June 10, 2008 Author Share Posted June 10, 2008 Logical.. and I knew that it was something I did that broke it! Thank you for making sure I "got it" Quote Link to comment 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.