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

sort() and an array. Quick question.

P: 26
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_rate" 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.
Dec 20 '07 #1
Share this Question
Share on Google+
5 Replies


pbmods
Expert 5K+
P: 5,821
Heya, Anthony.

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

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

Expand|Select|Wrap|Line Numbers
  1. $__map = array();
  2. foreach( $array as $__key => $__subArray )
  3. {
  4.     $__map[$__key] = $__subArray['success_rate'];
  5. }
  6.  
Then, pass the array to array_multisort() like this:
Expand|Select|Wrap|Line Numbers
  1. array_multisort($__map, SORT_DESC, $array);
  2.  
Dec 22 '07 #2

Markus
Expert 5K+
P: 6,050
Hey pbmods

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

:)
Dec 22 '07 #3

pbmods
Expert 5K+
P: 5,821
Heya, Markus.

Check out this thread.
Dec 23 '07 #4

P: 26
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 :)
Dec 24 '07 #5

pbmods
Expert 5K+
P: 5,821
Heya, Anthony.

Yeah, array_multisort() is pretty confusing.

Good luck with your project, and if you run into any trouble, post back anytime :)
Dec 24 '07 #6

Post your reply

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