rurouni Posted September 11, 2008 Share Posted September 11, 2008 Hi Guys: I got a array: $aaa = array( array('id'=>1,'name'=>aaa,'pld'=>1), array('id'=>1,'name'=>aaa,'pld'=>2), array('id'=>3,'name'=>ccc,'pld'=>1), array('id'=>2,'name'=>bbb,'pld'=>1), array('id'=>1,'name'=>aaa,'pld'=>3), array('id'=>3,'name'=>ccc,'pld'=>2), array('id'=>2,'name'=>bbb,'pld'=>2), array('id'=>3,'name'=>ccc,'pld'=>3), array('id'=>2,'name'=>bbb,'pld'=>3) ); Finally, I would make it looks like: $aaa = array( array('id'=>1,'name'=>aaa,'pld'=>6), array('id'=>2,'name'=>bbb,'pld'=>6), array('id'=>3,'name'=>ccc,'pld'=>6) ); Here is my code : foreach($aaa as $key => $row) { $id[$key] = $row['id']; } array_multisort($id,SORT_ASC,$aaa); for($i=0;$i<=sizeof($aaa)-1;$i++) { if ($i == 0) { $final[$aaa[$i]['id']]['pld'] = $aaa[$i]['pld']; } elseif($aaa[$i]['id'] == $aaa[$i - 1]['id']) { $nom = $aaa[$i]['pld'] + $aaa[$i -1]['pld']; $final[$aaa[$i - 1]['id']]['id'] = $aaa[$i]['id']; $final[$aaa[$i - 1]['id']]['name'] = $aaa[$i]['name']; $final[$aaa[$i - 1]['id']]['pld'] = $nom; } else { $final[$aaa[$i]['id']]['pld'] = $aaa[$i]['pld']; } } The Result from my code is: Array ( [1] => Array ( [pld] => 5 [id] => 1 [name] => aaa ) [2] => Array ( [pld] => 5 [id] => 2 [name] => bbb ) [3] => Array ( [pld] => 5 [id] => 3 [name] => ccc ) ) Please help me with this, cheers Link to comment https://forums.phpfreaks.com/topic/123763-solved-array-self-merge-problem/ Share on other sites More sharing options...
GingerRobot Posted September 11, 2008 Share Posted September 11, 2008 Bit confused as to what you're actually trying to achieve. Are you trying to find the sum of the 'pld' for each different id? Or each different name? Or something entirely different? Link to comment https://forums.phpfreaks.com/topic/123763-solved-array-self-merge-problem/#findComment-639047 Share on other sites More sharing options...
rurouni Posted September 11, 2008 Author Share Posted September 11, 2008 Bit confused as to what you're actually trying to achieve. Are you trying to find the sum of the 'pld' for each different id? Or each different name? Or something entirely different? Hi Thanks for the reply, The question is to find the sum of 'pld', when 'id' and 'name' are the same. cheers Link to comment https://forums.phpfreaks.com/topic/123763-solved-array-self-merge-problem/#findComment-639141 Share on other sites More sharing options...
rurouni Posted September 11, 2008 Author Share Posted September 11, 2008 Hi Guys: I got a array: I would like to find the sum of 'pld', when 'id' and 'name' are the same. $aaa = array( array('id'=>1,'name'=>aaa,'pld'=>1), array('id'=>1,'name'=>aaa,'pld'=>2), array('id'=>3,'name'=>ccc,'pld'=>1), array('id'=>2,'name'=>bbb,'pld'=>1), array('id'=>1,'name'=>aaa,'pld'=>3), array('id'=>3,'name'=>ccc,'pld'=>2), array('id'=>2,'name'=>bbb,'pld'=>2), array('id'=>3,'name'=>ccc,'pld'=>3), array('id'=>2,'name'=>bbb,'pld'=>3) ); Finally, I would make it looks like: $aaa = array( array('id'=>1,'name'=>aaa,'pld'=>6), array('id'=>2,'name'=>bbb,'pld'=>6), array('id'=>3,'name'=>ccc,'pld'=>6) ); Here is my code : foreach($aaa as $key => $row) { $id[$key] = $row['id']; } array_multisort($id,SORT_ASC,$aaa); for($i=0;$i<=sizeof($aaa)-1;$i++) { if ($i == 0) { $final[$aaa[$i]['id']]['pld'] = $aaa[$i]['pld']; } elseif($aaa[$i]['id'] == $aaa[$i - 1]['id']) { $nom = $aaa[$i]['pld'] + $aaa[$i -1]['pld']; $final[$aaa[$i - 1]['id']]['id'] = $aaa[$i]['id']; $final[$aaa[$i - 1]['id']]['name'] = $aaa[$i]['name']; $final[$aaa[$i - 1]['id']]['pld'] = $nom; } else { $final[$aaa[$i]['id']]['pld'] = $aaa[$i]['pld']; } } The Result from my code is: Array ( [1] => Array ( [pld] => 5 [id] => 1 [name] => aaa ) [2] => Array ( [pld] => 5 [id] => 2 [name] => bbb ) [3] => Array ( [pld] => 5 [id] => 3 [name] => ccc ) ) Please help me with this, cheers Link to comment https://forums.phpfreaks.com/topic/123763-solved-array-self-merge-problem/#findComment-639142 Share on other sites More sharing options...
sasa Posted September 11, 2008 Share Posted September 11, 2008 try <?php $aaa = array( array('id'=>1,'name'=>'aaa','pld'=>1), array('id'=>1,'name'=>'aaa','pld'=>2), array('id'=>3,'name'=>'ccc','pld'=>1), array('id'=>2,'name'=>'bbb','pld'=>1), array('id'=>1,'name'=>'aaa','pld'=>3), array('id'=>3,'name'=>'ccc','pld'=>2), array('id'=>2,'name'=>'bbb','pld'=>2), array('id'=>3,'name'=>'ccc','pld'=>3), array('id'=>2,'name'=>'bbb','pld'=>3) ); $out = array(); foreach ($aaa as $a){ $out[$a['id']][$a['name']] = isset($out[$a['id']][$a['name']]) ? $out[$a['id']][$a['name']] + $a['pld'] : $a['pld']; } $aaa = array(); foreach ($out as $id => $a){ foreach ($a as $name => $pld) $aaa[] = array('id' => $id, 'name' => $name, 'pld' => $pld); } sort($aaa); print_r($aaa); ?> Link to comment https://forums.phpfreaks.com/topic/123763-solved-array-self-merge-problem/#findComment-639219 Share on other sites More sharing options...
rurouni Posted September 12, 2008 Author Share Posted September 12, 2008 Thanks a lot, you save the day. cheers Link to comment https://forums.phpfreaks.com/topic/123763-solved-array-self-merge-problem/#findComment-639613 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.