sort() and an array. Quick question.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Anthony2oo5
    New Member
    • Jun 2007
    • 26

    sort() and an array. Quick question.

    Hello people, just a quick question I have here.

    My array looks like this:

    [PHP]Array (
    [0] => Array (
    [id] => 15
    [site_url] => example.com
    [clean_url] => examplecom
    [code] => NshKsi23
    [success] => 1
    [fail] => 1
    [success_rate] => 50
    [fail_rate] => 50
    [description] => Free delivery.
    [validfrom] => Not Set
    [validto] => 18 / 02 / 10 )
    [1] => Array (
    [id] => 14
    [site_url] => example.com
    [clean_url] => examplecom
    [code] => NshKsi23
    [success] => 2
    [fail] => 0
    [success_rate] => 100
    [fail_rate] => 0
    [description] => free delivery.
    [validfrom] => 01 / 02 / 05
    [validto] => 14 / 04 / 08 )
    [2] => Array (
    [id] => 13
    [site_url] => example.com
    [clean_url] => examplecom
    [code] => NshKsi23
    [success] => 2
    [fail] => 0
    [success_rate] => 100
    [fail_rate] => 0
    [description] => free delivery.
    [validfrom] => Not Set
    [validto] => Not Set)
    ) [/PHP]

    I was wondering is there a way to sort this data by the "success_ra te" in each of those arrays. So the data would be displayed like so:

    [PHP]Array (
    [0] => Array (
    [id] => 14
    [site_url] => example.com
    [clean_url] => examplecom
    [code] => NshKsi23
    [success] => 2
    [fail] => 0
    [success_rate] => 100
    [fail_rate] => 0
    [description] => free delivery.
    [validfrom] => 01 / 02 / 05
    [validto] => 14 / 04 / 08 )
    [1] => Array (
    [id] => 13
    [site_url] => example.com
    [clean_url] => examplecom
    [code] => NshKsi23
    [success] => 2
    [fail] => 0
    [success_rate] => 100
    [fail_rate] => 0
    [description] => free delivery.
    [validfrom] => Not Set
    [validto] => Not Set)
    [2] => Array (
    [id] => 15
    [site_url] => example.com
    [clean_url] => examplecom
    [code] => NshKsi23
    [success] => 1
    [fail] => 1
    [success_rate] => 50
    [fail_rate] => 50
    [description] => Free delivery.
    [validfrom] => Not Set
    [validto] => 18 / 02 / 10 )
    ) [/PHP]

    Thanks a lot if anyone can help me.

    EDIT: and just so you know, the success_rate is worked out in PHP and is not stored in the database so I can not use ORDER BY in my MySQL statement. Thanks for your time.
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, Anthony.

    To do this, you need to use array_multisort ().

    First, you need to create a temporary array that maps keys to values:

    [code=php]
    $__map = array();
    foreach( $array as $__key => $__subArray )
    {
    $__map[$__key] = $__subArray['success_rate'];
    }
    [/code]

    Then, pass the array to array_multisort () like this:
    [code=php]
    array_multisort ($__map, SORT_DESC, $array);
    [/code]

    Comment

    • Markus
      Recognized Expert Expert
      • Jun 2007
      • 6092

      #3
      Hey pbmods

      i was just wondering, why do you do variables like so:
      [php]
      $__someVariable
      [/php]

      :)

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        Heya, Markus.

        Check out this thread.

        Comment

        • Anthony2oo5
          New Member
          • Jun 2007
          • 26

          #5
          Thanks and sorry for the late reply.

          I will have a mess with your method after Christmas and try to implement it. I looked through the different sorts on the php website but things got confusing lol.

          Thanks very much for your time :)

          Comment

          • pbmods
            Recognized Expert Expert
            • Apr 2007
            • 5821

            #6
            Heya, Anthony.

            Yeah, array_multisort () is pretty confusing.

            Good luck with your project, and if you run into any trouble, post back anytime :)

            Comment

            Working...