473,382 Members | 1,348 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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_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
5 1493
pbmods
5,821 Expert 4TB
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
6,050 Expert 4TB
Hey pbmods

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

:)
Dec 22 '07 #3
pbmods
5,821 Expert 4TB
Heya, Markus.

Check out this thread.
Dec 23 '07 #4
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
5,821 Expert 4TB
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

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

Similar topics

1
by: Shaunak Kashyap | last post by:
Does anyone know what sorting algorithm(s) -- quicksort, mergesort, radix sort, etc. -- does PHP use internally in its sort function?
12
by: Eva | last post by:
Hi, I try to implement quick sort. I sort vectors by their first value. 10 2 3 4 9 3 5 6 10 4 5 6 must be 9 3 5 6 10 2 3 4 10 4 5 6 The prog works great on maybe 500 vectors, but I have an...
5
by: GIMME | last post by:
My question is ... How do I sort an Array on numeric, not character values ? In the example below, after sorting the contents are 1,10,2,3 . How do I get the contents to be 1,2,3,10 ? ...
0
by: Frank King | last post by:
Hi, I am using CArray and quick sort funciton to sort an array of double type of data points. I found an article in MSDN HOWTO: Quick Sorting Using MFC CArray-Derived Classes ID: Q216858 ...
16
by: Gerrit | last post by:
Hello, Is it possible to sort an array with a struct in it? example: I have a struct: public struct mp3file { public int tracknr;
48
by: Alex Chudnovsky | last post by:
I have come across with what appears to be a significant performance bug in ..NET 2.0 ArrayList.Sort method when compared with Array.Sort on the same data. Same data on the same CPU gets sorted a...
13
by: ralphedge | last post by:
These sorts work fine on 100000 ints but if I go much higher they will both segmentation fault **************************MERGESORT********************* mergesort(int *a, int size) //a is...
0
by: JosAH | last post by:
Greetings, I was asked to write a Tip Of the Week; so here goes: a lot of topics are started here in this forum (and a lot of other forums too) mentioning a problem about sorting data. ...
5
by: feverzsj | last post by:
STL sort() as an implementation of introsort, below is the code snippet from the gnu stl. template<typename _RandomAccessIterator, typename _Size> void __introsort_loop(_RandomAccessIterator...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.