AndyPSV Posted February 2, 2011 Share Posted February 2, 2011 array ( 0 => array ( 'c0' => '6/111', 'c1' => '6/114', 'cnt' => '1', ), 1 => array ( 'c0' => '6/114', 'c1' => '6/111', 'cnt' => '1', ), 2 => array ( 'c0' => '6/145', 'c1' => '6/116', 'cnt' => '1', ), ) I want to "match" as on example code 0 => array ( 'c0' => '6/111', 'c1' => '6/114', 'cnt' => '1', ), 1 => array ( 'c0' => '6/114', 'c1' => '6/111', 'cnt' => '1', ), c0 & c1, to get (and the array to be reseted) 0 => array ( 'c0' => '6/111', 'c1' => '6/114', 'cnt' => '2', ), 1 => array ( 'c0' => '6/145', 'c1' => '6/116', 'cnt' => '1', ), ) how to do it? I think that on only SQL it's impossible to do that ("switch"), furthermore I think that the code above would be useful /* $_c1 = @array_merge($_c1,$_c1x); if(!empty($_c0)) { foreach($_c1 as $value) { if(!isset($c1x[$value['id']])) { $c1x[$value['id']] = $value; $c1x[$value['id']]['cnt'] = 0; } $c1x[$value['id']]['cnt']++; } $this->tpl->assign('c1x',$c1x); } */ please help mi, thank you very much Quote Link to comment Share on other sites More sharing options...
AndyPSV Posted February 2, 2011 Author Share Posted February 2, 2011 How can I edit my post? Anyone could help me with this issue? Quote Link to comment Share on other sites More sharing options...
btherl Posted February 3, 2011 Share Posted February 3, 2011 So you want to match entries where c0 and c1 are switched? It can be done in SQL, but it's a bit tricky. It's a bit tricky in php as well. In SQL you could do something like this: SELECT (CASE WHEN c0 > c1 THEN c0 ELSE c1 END) AS c0 , (CASE WHEN c0 > c1 THEN c1 ELSE c0 END) AS c1 , COUNT(*) AS cnt FROM table GROUP BY c0, c1 What this does is it switches c0 and c1 if c0 > c1, but leaves them unswitched if c0 == c1 or c0 < c1. The end result will have all the c0 and c1 in the same order. Then "GROUP BY" can aggregate the results without worrying about switching. Quote Link to comment Share on other sites More sharing options...
AndyPSV Posted February 3, 2011 Author Share Posted February 3, 2011 You're a master guru Thanks! On the rest I would handle myself, thank you. 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.