php - Put multiple arrays in one large associative array -
i creating set of arrays following loop:
$assessmentarr = explode("&", $assessmentdata); foreach($assessmentarr $data) { $fullarr = explode("_", $data); // break down archetype , value $resultarr = explode("=", $fullarr[2]); //print_r($resultarr); } which produces following results:
array ( [0] => community-support [1] => 24 ) array ( [0] => money-rewards [1] => 30 ) array ( [0] => status-stability [1] => 15 ) array ( [0] => personal-professional-development [1] => 32 ) array ( [0] => community-support [1] => 9 ) array ( [0] => money-rewards [1] => 12 ) array ( [0] => status-stability [1] => 16 ) array ( [0] => personal-professional-development [1] => 29 ) i need combine these 1 array, , [0] value matches, need add [1] value together.
so final output like:
array ( [community-support] => 33 [money-rewards] => 42 [status-stability] => 31 [personal-professional-development] => 61 ) i found question: how merge 2 arrays summing merged values assist me in merging , adding values together, i'm not sure how go when arrays aren't assigned variable. trying possible or going wrong way?
don't make complicated, check if results array has element key , if not initialize otherwise add it. e.g.
(add code in loop):
if(!isset($result[$resultarr[0]])) $result[$resultarr[0]] = $resultarr[1]; else $result[$resultarr[0]] += $resultarr[1]; then have desired array:
print_r($result);
Comments
Post a Comment