Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

multidimensional array, Sort column

Status
Not open for further replies.

Zeroanarchy

Technical User
Jun 11, 2001
630
AU
Okay I have tried numerous functional examples and have been at it for two days now. I seem to be having an issue sorting a column.

Here is the output via print_r
------------------------------------------------------
Array ( [0] => A0034 [1] => 4 [2] => 1 )
Array ( [0] => A0035 [1] => 18 , 9 , 14 [2] => 3 )
Array ( [0] => A0036 [1] => 9 [2] => 1 )
Array ( [0] => A0037 [1] => 14 , 18 [2] => 2 )
Array ( [0] => A0039 [1] => 8 , 9 , 14 [2] => 3 )
Array ( [0] => A0040 [1] => 18 , 9 [2] => 2 )
Array ( [0] => A0041 [1] => 8 , 6 [2] => 2 )
Array ( [0] => A0042 [1] => 6 [2] => 1 )
Array ( [0] => A0045 [1] => 9 [2] => 1 )
Array ( [0] => A0046 [1] => 4 , 6 [2] => 2 )
Array ( [0] => A0047 [1] => 18 [2] => 1 )
Array ( [0] => A0048 [1] => 14 [2] => 1 )
Array ( [0] => A0049 [1] => 6 , 14 , 8 , 18 [2] => 4 )
Array ( [0] => A0050 [1] => 14 [2] => 1 )

------------------------------------------------------
I am trying to sort column[2] in descending order. I have tried several of the examples here: and have looked through a stack of post at tek-tips and none of theme seem to produce the desired results.

CODE SNIPET
------------------------------------------------------
if (count(array_intersect($selectedno, $numbers)) >= 1) {
$rs = array(($a_row['selectedno']), join(" , ",array_intersect($selectedno, $numbers)), count(array_intersect($selectedno, $numbers)));
echo "<br />";
} else {
$rs = null;
}
print_r ($rs);
------------------------------------------------------

Thanks in advance.

[afro]ZeroAnarchy
Experience is a wonderful thing. It enables you to recognize a mistake
when you make it again.
 
Hi

How you tried to use [tt]array_multisort()[/tt] ? There is no such call in you sample code.

Please post your code between [tt][ignore]
Code:
[/ignore][/tt] and [tt][ignore]
[/ignore][/tt] TGML tags. Makes it easier to read.

Feherke.
 
Thanks feherke for you contribution.

I have tried the array_multisort as follows and the results were not as I expected.

Code:
if (count(array_intersect($draw, $numbers)) >= 1) {
									$rs = array(($a_row['DrawNo']), join(" , ",array_intersect($draw, $numbers)), count(array_intersect($draw, $numbers)));
									echo  "<br />";
									} else {
									$rs = null;	
							}
							array_multisort($rs[1], SORT_NUMERIC, SORT_DESC, $rs[0], SORT_ASC, SORT_STRING);
							var_dump($rs);

OUTPUT
Array ( [0] => A0034 [1] => 4 [2] => 1 )
Array ( [0] => A0035 [1] => 18 , 9 , 14 [2] => 3 )
Array ( [0] => A0036 [1] => 9 [2] => 1 )
Array ( [0] => A0037 [1] => 14 , 18 [2] => 2 ) Array ( [1] => [0] => )
Array ( [0] => A0039 [1] => 8 , 9 , 14 [2] => 3 )
Array ( [0] => A0040 [1] => 18 , 9 [2] => 2 )
Array ( [0] => A0041 [1] => 8 , 6 [2] => 2 )
Array ( [0] => A0042 [1] => 6 [2] => 1 ) Array ( [1] => [0] => ) Array ( [1] => [0] => )
Array ( [0] => A0045 [1] => 9 [2] => 1 )
Array ( [0] => A0046 [1] => 4 , 6 [2] => 2 )
Array ( [0] => A0047 [1] => 18 [2] => 1 )
Array ( [0] => A0048 [1] => 14 [2] => 1 )
Array ( [0] => A0049 [1] => 6 , 14 , 8 , 18 [2] => 4 )
Array ( [0] => A0050 [1] => 14 [2] => 1 )

I also tried

Code:
	Function compare($x, $y)
						{
							if ( $x[0] == $y[0] )
							return 0;
							else if ( $x[0] < $y[0] )
							return -1;
							else
							return 1;
						}


	if (count(array_intersect($draw, $numbers)) >= 1) {
									$rs = array(($a_row['DrawNo']), join(" , ",array_intersect($draw, $numbers)), count(array_intersect($draw, $numbers)));
									echo  "<br />";
									} else {
									$rs = 0;	
							}
								usort($rs, 'compare');
								var_dump($rs);

OUTPUT
array(3) { [0]=> int(1) [1]=> string(1) "4" [2]=> string(5) "A0034" }
array(3) { [0]=> int(3) [1]=> string(11) "18 , 9 , 14" [2]=> string(5) "A0035" }
array(3) { [0]=> int(1) [1]=> string(1) "9" [2]=> string(5) "A0036" }
array(3) { [0]=> int(2) [1]=> string(7) "14 , 18" [2]=> string(5) "A0037" } int(0)
array(3) { [0]=> int(3) [1]=> string(10) "8 , 9 , 14" [2]=> string(5) "A0039" }
array(3) { [0]=> int(2) [1]=> string(6) "18 , 9" [2]=> string(5) "A0040" }
array(3) { [0]=> int(2) [1]=> string(5) "8 , 6" [2]=> string(5) "A0041" }
array(3) { [0]=> int(1) [1]=> string(1) "6" [2]=> string(5) "A0042" } int(0) int(0)
array(3) { [0]=> int(1) [1]=> string(1) "9" [2]=> string(5) "A0045" }
array(3) { [0]=> int(2) [1]=> string(5) "4 , 6" [2]=> string(5) "A0046" }
array(3) { [0]=> int(1) [1]=> string(2) "18" [2]=> string(5) "A0047" }
array(3) { [0]=> int(1) [1]=> string(2) "14" [2]=> string(5) "A0048" }
array(3) { [0]=> int(4) [1]=> string(15) "6 , 14 , 8 , 18" [2]=> string(5) "A0049" }
array(3) { [0]=> int(1) [1]=> string(2) "14" [2]=> string(5) "A0050" }

I have tried other solutions but for some reason they only want to order the rows not the columns.

Cheers


[afro]ZeroAnarchy
Experience is a wonderful thing. It enables you to recognize a mistake
when you make it again.

 
i have not given this much time (apologies) but would this code not work?

Code:
<?php

	$cols = count ($array[0]);
	foreach ($array as $key => $row){
		for($i=0; $i<$cols; $i++){
			$cols_{$i}[$key] = $row[$i];
		}
	}
	
	array_multisort($cols_2, SORT_DESC, $cols_0, SORT_ASC, $cols_1 );
	print_r(array($cols_0, $cols_1, $cols_2));
?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top