[PHP] Re: Sorting multidim array and keeping associations

2004-12-11 Thread Peter Lauri
Stupid as I was, I did not do what I should have done :) In the origin the
array was created from a MySQL database. I did not sort it correctly from
there because I did not have the knowledge about the GROUP BY command, so I
tried to solve it via PHP. Now I have everything sorted and alright just by
making the Query correct.

/Peter


Peter Lauri [EMAIL PROTECTED] skrev i meddelandet
news:[EMAIL PROTECTED]
 Best groupmember,

 I have an multidim array that looks something like this:

 [40] = [1]=32, [2]=55, [total]=87
 [22] = [8]=2, [7]=105, [total]=107
 [142] = [2]=3, [7]=8, [total]=11

 I want to sort this array according to the total and still keep the acc.
 with the basekey. I know I can easily do this by writing an function
myself.
 But are there any built in functions for this? I have looked at usort,
 uksort and more, but they do not seem to solve my problem.

 - Best Of Times
 /Peter

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Re: Sorting multidim array and keeping associations

2004-11-18 Thread Sebastian Mendel
Peter Lauri wrote:
I have an multidim array that looks something like this:
[40] = [1]=32, [2]=55, [total]=87
[22] = [8]=2, [7]=105, [total]=107
[142] = [2]=3, [7]=8, [total]=11
I want to sort this array according to the total and still keep the
acc. with the basekey. I know I can easily do this by writing an function 
myself.
But are there any built in functions for this? I have looked at usort,
uksort and more, but they do not seem to solve my problem.
uksort() or array_multisort() should do fine
I have tried to figure it out how to use it with those, but I can not find a
solution.
and whats the problem? did you tried uksort() ?
what did you tried? post the code, and someone will take a look and give 
a hint!

sorry, but if you want a complete solution, go hire a php-programmer!
--
Sebastian Mendel
www.sebastianmendel.de www.warzonez.de www.tekkno4u.de www.nofetish.com
www.sf.net/projects/phpdatetimewww.sf.net/projects/phptimesheet
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[PHP] Re: Sorting multidim array and keeping associations

2004-11-18 Thread Peter Lauri
The problem was more complex than I described, I instead wrote a sorting
algorithm for this specific purpose.

The array multi-dim array looks like this (short version). The total element
is just the sum of the other. This is a point system for golftournaments. I
wanted to sort them first on total and if the total was the same I wanted
to sort them with the highest point in a specific tournament, and so on. I
have not found an simple solution to this using any built in functions. So I
thought is was the best to sort it manually. My algorithm is problably not
efficient, but it works just fine for this solution. You can find the
algorithm in the end of this message.

/Peter
Array
(
[40] = Array
(
[1] = 16
[2] = 20
[3] = 20
[4] = 10
[total] = 66
)

[35] = Array
(
[1] = 20
[2] = 11
[3] = 12
[4] = 20
[5] = 17
[total] = 80
)

[49] = Array
(
[2] = 14
[total] = 14
)

[139] = Array
(
[4] = 14
[total] = 14
)

[79] = Array
(
[4] = 10
[5] = 4
[total] = 14
)
)

//Sorting Order Of Merit arrayfunction sort_oom($arr) { //create the sum
element by adding all tournament result foreach($arr as $id = $player) {
$total=0;  foreach($player as $tourpoint) {   $total+=$tourpoint;  }
$arr[$id]['total']=$total; }  reset($arr); $num = count($arr);
$sortedarray=array(); for($i=1; $i=$num; $i++) {  //Get the element that
ranks currently #1  $elem = get_largest_element($arr, 'total');  //Put that
element into a new array  $sortedarray[$elem]=$arr[$elem];  //Unset the
element from the original array  unset($arr[$elem]);  //reset the array
reset($arr); } //Viola, the array is sorted... return $sortedarray; }
function get_largest_element($arr, $sortkey) { //create candidatearray $cand
= array(); //before looping the largest total is 0 $largest = 0;
reset($arr); foreach($arr as $id = $value) {  if($value[$sortkey] 
$largest) {   //Delete all candidates entered because they are not
candidates to be largest no more   unset($cand);   //Insert into
candidatearray   $cand[$id]=$value;   //update the largest value   $largest
= $value[$sortkey];  } elseif($value[$sortkey] == $largest) {   //Add to
candidate key   $cand[$id]=$value;  } } $arg=true; $largest=0; reset($cand);
//Candidate contains at minimum 1 candidate //If more than two candidates,
subsorting have to begin while(count($cand)1 AND $arg) {  //Find the
largest subvalue (all values not $sortkey)  foreach($cand as $id = $value)
{   foreach($value as $secid = $point) {if($point  $largest AND
$secid!=$sortkey) { $largest=$point;}   }  }  //Loop thru array and
delete elements that do not have  //values larger or equal to the largest
value  foreach($cand as $id = $value) {   $ok=false;   foreach($value as
$secid = $point) {if($point == $largest AND $secid!=$sortkey) {
unset($cand[$id][$secid]); $ok=true;}   }   if(!$ok AND
count($cand[$id])1) { unset($cand[$id]);}  }  //If there are candidates
presice equal, pick the first.  if($largest==0 AND count($cand)1) {
$arg=false;   $check = 1;   foreach($cand as $id = $value) {
if($check!=1) { unset($cand[$id]);}$check++;   }  }
 $largest=0; } reset($cand); $el = each($cand); return $el['key'];}

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Re: Sorting multidim array and keeping associations

2004-11-18 Thread Sebastian Mendel
Peter Lauri wrote:
The problem was more complex than I described, I instead wrote a sorting
algorithm for this specific purpose.
The array multi-dim array looks like this (short version). The total element
is just the sum of the other. This is a point system for golftournaments. I
wanted to sort them first on total and if the total was the same I wanted
to sort them with the highest point in a specific tournament, and so on. I
have not found an simple solution to this using any built in functions. So I
thought is was the best to sort it manually. My algorithm is problably not
efficient, but it works just fine for this solution. You can find the
algorithm in the end of this message.
Array
(
[40] = Array
(
[1] = 16
[2] = 20
[3] = 20
[4] = 10
[total] = 66
)
[49] = Array
(
[2] = 14
[total] = 14
)
)
// without totals
$score_board = uksort( $score_board, 'sortScoreBoard' );
function sortScoreBoard( $element_1, $element_2 )
{
   if ( array_sum( $element_1 )  array_sum( $element_2 ) ) {
   return -1;
   }
   elseif ( array_sum( $element_1 )  array_sum( $element_2 ) ) {
   return 1;
   }
   elseif ( max( $element_1 )  max( $element_2 ) ) {
   return -1;
   }
   elseif ( max( $element_1 )  max( $element_2 ) ) {
   return 1;
   }
   return 0;
}
--
Sebastian Mendel
www.sebastianmendel.de www.warzonez.de www.tekkno4u.de www.nofetish.com
www.sf.net/projects/phpdatetimewww.sf.net/projects/phptimesheet
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[PHP] Re: Sorting multidim array and keeping associations

2004-11-17 Thread Sebastian Mendel
Peter Lauri wrote:
Best groupmember,
I have an multidim array that looks something like this:
[40] = [1]=32, [2]=55, [total]=87
[22] = [8]=2, [7]=105, [total]=107
[142] = [2]=3, [7]=8, [total]=11
I want to sort this array according to the total and still keep the acc.
with the basekey. I know I can easily do this by writing an function myself.
But are there any built in functions for this? I have looked at usort,
uksort and more, but they do not seem to solve my problem.
uksort() or array_multisort() should do fine
--
Sebastian Mendel
www.sebastianmendel.de www.warzonez.de www.tekkno4u.de www.nofetish.com
www.sf.net/projects/phpdatetimewww.sf.net/projects/phptimesheet
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[PHP] Re: Sorting multidim array and keeping associations

2004-11-17 Thread Peter Lauri
I have tried to figure it out how to use it with those, but I can not find a
solution.

- Best Of Times
/Peter


Sebastian Mendel [EMAIL PROTECTED] skrev i meddelandet
news:[EMAIL PROTECTED]
 Peter Lauri wrote:
  Best groupmember,
 
  I have an multidim array that looks something like this:
 
  [40] = [1]=32, [2]=55, [total]=87
  [22] = [8]=2, [7]=105, [total]=107
  [142] = [2]=3, [7]=8, [total]=11
 
  I want to sort this array according to the total and still keep the
acc.
  with the basekey. I know I can easily do this by writing an function
myself.
  But are there any built in functions for this? I have looked at usort,
  uksort and more, but they do not seem to solve my problem.

 uksort() or array_multisort() should do fine

 --
 Sebastian Mendel

 www.sebastianmendel.de www.warzonez.de www.tekkno4u.de www.nofetish.com
 www.sf.net/projects/phpdatetimewww.sf.net/projects/phptimesheet

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Re: Sorting multidim array and keeping associations

2004-11-17 Thread Peter Lauri
I have tried to figure it out how to use it with those, but I can not find a
solution.

- Best Of Times
/Peter


Sebastian Mendel [EMAIL PROTECTED] skrev i meddelandet
news:[EMAIL PROTECTED]
 Peter Lauri wrote:
  Best groupmember,
 
  I have an multidim array that looks something like this:
 
  [40] = [1]=32, [2]=55, [total]=87
  [22] = [8]=2, [7]=105, [total]=107
  [142] = [2]=3, [7]=8, [total]=11
 
  I want to sort this array according to the total and still keep the
acc.
  with the basekey. I know I can easily do this by writing an function
myself.
  But are there any built in functions for this? I have looked at usort,
  uksort and more, but they do not seem to solve my problem.

 uksort() or array_multisort() should do fine

 --
 Sebastian Mendel

 www.sebastianmendel.de www.warzonez.de www.tekkno4u.de www.nofetish.com
 www.sf.net/projects/phpdatetimewww.sf.net/projects/phptimesheet

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php