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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • panos100m
    New Member
    • Aug 2007
    • 23

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

    Hi


    I can't get the following to work:
    [code=php]
    //that is in the for loop only it works just an example of the array
    $users[$i] = array( 'username' => $unique[$i], 'times' => $k );

    }




    $users[3] = array( 'username' => "testuser", 'times' => 8000 ); //add use


    echo $users[0]['times'];


    for ($l=0;$l<sizeof ($users);$l++)

    for ($k=0;$k<sizeof ($users);$k++)
    {
    echo $users[$k+1]('username''tim es);
    if ($users[$k]['times'] < $users[$k+1]['times'])
    {

    $temp[0]['username']['times']= $users[$k]['username']['times'];
    $users[$k]['username']['times']=$users[$k+1]['username']['times'];
    $users[$k+1]['username']['times']=$temp[0]['username']['times'];
    }

    }
    [/code]
    Any idea why the array doesnt get sorted :-( ??

    Thanks

    Panos
    Last edited by Atli; Sep 20 '08, 12:12 PM. Reason: Added [code] tags.
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Panos.

    Please use &#91;code] tags when posting your code examples.

    &#91;code] ...Code goes here... [/code]
    &#91;php] ...PHP code goes here... [/php]

    Thank you
    Moderator

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      This code makes no sense.

      You create an array that looks like this:
      Code:
      array(
        array(
          "username" => "string",
          "times" => int
        )
      );
      And then you try to compare this element:
      [code=php]
      $array[n]['username']['times']
      // n being an integer value
      [/code]
      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?

      Comment

      • panos100m
        New Member
        • Aug 2007
        • 23

        #4
        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

        Code:
        $users[0] = array( 'username' => "username1", 'times' => 222 );
        $users[1] = array( 'username' => "username2", 'times' => 333 );
        $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

        Code:
        for ($l=0;$l<sizeof($users);$l++)
        
        for ($k=0;$k<sizeof($users);$k++)
        {
        //echo $users[$k+1]('username''times);
        if ($users[$k]['times'] < $users[$k+1]['times'])
        {
        
             $temp[0]['times']=  $users[$k]['times'];
        	 $temp[0]['username']=  $users[$k]['username'];
        $users[$k]['username']=$users[$k+1]['username'];
        $users[$k]['times']=$users[$k+1]['times'];
        
        $users[$k+1]['username']=$temp[0]['username'];
        $users[$k+1]['times']=$temp[0]['times'];
        }
        
        }
        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 .

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          Originally posted by panos100m
          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

          Code:
          $users[0] = array( 'username' => "username1", 'times' => 222 );
          $users[1] = array( 'username' => "username2", 'times' => 333 );
          $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

          Comment

          Working...