473,503 Members | 1,888 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

23 New Member
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
4 1403
Atli
5,058 Recognized Expert Expert
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
5,058 Recognized Expert Expert
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
panos100m
23 New Member
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
8,658 Recognized Expert Moderator Expert
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

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

Similar topics

9
2553
by: jwedel_stolo | last post by:
Hi I'm creating a dataview "on the fly" in order to sort some data prior to writing out the information to a MS SQL table I have used two methods in order to determine the sort order of the...
12
2365
by: pmud | last post by:
Hi, I am using teh following code for sorting the data grid but it doesnt work. I have set the auto generate columns to false. & set the sort expression for each field as the anme of that...
7
3235
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) ...
4
2520
by: John Bullock | last post by:
Hello, I am at wit's end with an array sorting problem. I have a simple table-sorting function which must, at times, sort on columns that include entries with nothing but a space (@nbsp;). I...
47
5028
by: VK | last post by:
Or why I just did myArray = "Computers" but myArray.length is showing 0. What a hey? There is a new trend to treat arrays and hashes as they were some variations of the same thing. But they...
4
2343
by: Mark Travis | last post by:
Hi all, I have written a simple Web Application that displays a query result onto a page using the ASP DataGrid. To Digress ======= Development information about the page is as follows 1....
5
1461
by: Chris H | last post by:
Okay, I am trying to us a varialble with a list of number values as my data for an array, yet when I do this it doesnt return any values from the array, i was able to temporarily fix the proble by...
41
4874
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in...
1
2982
by: Totti | last post by:
Hi everyone, I am a newbie to javascript learning it for 2 months now, i am trying to make a sorter who will read english words from the one textbox or datafile and store them in a 2 dimensional...
0
7205
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7287
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7348
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7006
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5592
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4685
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3166
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1519
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.