By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,723 Members | 1,891 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,723 IT Pros & Developers. It's quick & easy.

Sorting of associative array doesnt work...what am i doing wrong here?

P: 23
Hi


I can't get the following to work:
Expand|Select|Wrap|Line Numbers
  1. //that is in the for loop only it works just an example of the array 
  2.                   $users[$i] = array( 'username' => $unique[$i], 'times' => $k );
  3.  
  4. }
  5.  
  6.  
  7.  
  8.  
  9.   $users[3] = array( 'username' => "testuser", 'times' => 8000 ); //add use
  10.  
  11.  
  12. echo $users[0]['times'];
  13.  
  14.  
  15. for ($l=0;$l<sizeof($users);$l++)
  16.  
  17. for ($k=0;$k<sizeof($users);$k++)
  18. {
  19. echo $users[$k+1]('username''times);
  20. if ($users[$k]['times'] < $users[$k+1]['times'])
  21. {
  22.  
  23.      $temp[0]['username']['times']=  $users[$k]['username']['times'];
  24. $users[$k]['username']['times']=$users[$k+1]['username']['times'];
  25. $users[$k+1]['username']['times']=$temp[0]['username']['times'];
  26. }
  27.  
  28. }
  29.  
Any idea why the array doesnt get sorted :-( ??

Thanks

Panos
Sep 20 '08 #1
Share this Question
Share on Google+
4 Replies


Atli
Expert 5K+
P: 5,058
Panos.

Please use [code] tags when posting your code examples.

[code] ...Code goes here... [/code]
[php] ...PHP code goes here... [/php]

Thank you
Moderator
Sep 20 '08 #2

Atli
Expert 5K+
P: 5,058
This code makes no sense.

You create an array that looks like this:
Expand|Select|Wrap|Line Numbers
  1. array(
  2.   array(
  3.     "username" => "string",
  4.     "times" => int
  5.   )
  6. );
  7.  
And then you try to compare this element:
Expand|Select|Wrap|Line Numbers
  1. $array[n]['username']['times']
  2. // n being an integer value
  3.  
Which doesn't exist.

And then, once that is supposed to be compared, you swap the current "times" element with the next "times" element, but leaving the "username" element in place.

Could you explain the thought behind this?
How is it not working?
What isn't it doing that it should be doing?
What is it doing that it shouldn't?

Are you getting any errors?
Have you turned on the debug messages?
Sep 20 '08 #3

P: 23
Hi i wanted to sort the results so the users that they are the most times in the list will appear in the first elements of the array:

so for a given array

Expand|Select|Wrap|Line Numbers
  1. $users[0] = array( 'username' => "username1", 'times' => 222 );
  2. $users[1] = array( 'username' => "username2", 'times' => 333 );
  3. $users[2] = array( 'username' => "username4", 'times' => 1 )
sorted it will hold

array[0]username2:333
array[1]username1:222
array[1]username4:1



i corrected my code and it works but there must be a more elegant way (less code)

....the way that it works is by checking the current element of the array with the next one and if it the current element is smaller it switches them position, with the help of another array with two associative fields one for times and one for user

Expand|Select|Wrap|Line Numbers
  1. for ($l=0;$l<sizeof($users);$l++)
  2.  
  3. for ($k=0;$k<sizeof($users);$k++)
  4. {
  5. //echo $users[$k+1]('username''times);
  6. if ($users[$k]['times'] < $users[$k+1]['times'])
  7. {
  8.  
  9.      $temp[0]['times']=  $users[$k]['times'];
  10.      $temp[0]['username']=  $users[$k]['username'];
  11. $users[$k]['username']=$users[$k+1]['username'];
  12. $users[$k]['times']=$users[$k+1]['times'];
  13.  
  14. $users[$k+1]['username']=$temp[0]['username'];
  15. $users[$k+1]['times']=$temp[0]['times'];
  16. }
  17.  
  18. }
However there must be a better way i guess? (less code for the same thing)

Was looking at a php function array multisort any ideas?

Thanks for the help btw .
Sep 20 '08 #4

Dormilich
Expert Mod 5K+
P: 8,639
Hi i wanted to sort the results so the users that they are the most times in the list will appear in the first elements of the array:

so for a given array

Expand|Select|Wrap|Line Numbers
  1. $users[0] = array( 'username' => "username1", 'times' => 222 );
  2. $users[1] = array( 'username' => "username2", 'times' => 333 );
  3. $users[2] = array( 'username' => "username4", 'times' => 1 )
sorted it will hold

array[0]username2:333
array[1]username1:222
array[1]username4:1

Was looking at a php function array multisort any ideas?
as you were proposing array_multisort() should do what you want, look at the examples there's described what you need (I think)

regards
Sep 21 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.