Jump to content

How to match particular (switched) records in array


AndyPSV

Recommended Posts

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

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.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.