473,837 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sorting a multi array

Hello All

I am trying to update me code to use arrays to store a group of information
and I have come up with a problem sorting the multiple array :(

Array trying to sort: (7 arrays put into an array) and I want to sort on
[count] Descending.
This is displayed using print_r() function.

Array
(
[czero] => Array
(
[game_type] => czero
[game_label] => Condition Zero
[count] => 2
)
[cstrike] => Array
(
[game_type] => cstrike
[game_label] => Counter-Strike
[count] => 3
)
[dod] => Array
(
[game_type] => dod
[game_label] => Day of Defeat
[count] => 1
)
[valve] => Array
(
[game_type] => valve
[game_label] => Half Life
[count] => 0
)
[gearbox] => Array
(
[game_type] => gearbox
[game_label] => Opposing Force
[count] => 0
)
[ricochet] => Array
(
[game_type] => ricochet
[game_label] => Ricochet
[count] => 0
)
[tfc] => Array
(
[game_type] => tfc
[game_label] => Team Fortress Classic
[count] => 1
)
)

Does anyone know how to do this ?
I have tried various array sorting functions without success :(

If you require more information, then let me know.

Thanks in advance
Paul Kirby
Jul 17 '05 #1
3 2988
"Paul Kirby" <ad***@initcorp .co.uk> wrote in message
news:ca******** ***********@new s.demon.co.uk.. .
Hello All

I am trying to update me code to use arrays to store a group of information and I have come up with a problem sorting the multiple array :(

Array trying to sort: (7 arrays put into an array) and I want to sort on
[count] Descending.
This is displayed using print_r() function.

Array
(
[czero] => Array
(
[game_type] => czero
[game_label] => Condition Zero
[count] => 2
)
[cstrike] => Array
(
[game_type] => cstrike
[game_label] => Counter-Strike
[count] => 3
)
[dod] => Array
(
[game_type] => dod
[game_label] => Day of Defeat
[count] => 1
)
[valve] => Array
(
[game_type] => valve
[game_label] => Half Life
[count] => 0
)
[gearbox] => Array
(
[game_type] => gearbox
[game_label] => Opposing Force
[count] => 0
)
[ricochet] => Array
(
[game_type] => ricochet
[game_label] => Ricochet
[count] => 0
)
[tfc] => Array
(
[game_type] => tfc
[game_label] => Team Fortress Classic
[count] => 1
)
)

Does anyone know how to do this ?
I have tried various array sorting functions without success :(


See www.php.net/uasort and associated functions.

- Virgil
Jul 17 '05 #2
I think I found a variant of this under the usort function:

$sorted_array = arfsort($array, 'count');
or
$sorted_array = arfsort($array, array('count')) ; // can pass secondary
sort field in the 2nd param..

to get descending:
$sorted_array = array_reverse(a rfsort($array,a rray('count'))) ;

//arfsort() - (AR)ray (F)ield Sort.
//Sort a multi-dimensional array according to a list of fields.
//preserves keys
function arfsort( $array, $fields ) {
debug('<B>arfso rt(</B><B>)</B> [1.00 - 2005/05/26]');
if ( !is_array($fiel ds) )
$fields = array($fields);
$GLOBALS['__ARFSORT_LIST __'] = $fields;
uasort( $array, 'arfsort_func' );
unset($GLOBALS['__ARFSORT_LIST __']);
return $array;
}
//Internal sorting function for arfsort()
function arfsort_func( $a1, $a2 ) {
foreach( $GLOBALS['__ARFSORT_LIST __'] as $f ) {
if ( isset($a1[$f]) && isset($a2[$f]) ) {
$strc = strnatcasecmp( $a1[$f], $a2[$f] );
return $strc;
}
}
return 0;
}

"Paul Kirby" <ad***@initcorp .co.uk> wrote in message news:<ca******* ************@ne ws.demon.co.uk> ...
Hello All

I am trying to update me code to use arrays to store a group of information
and I have come up with a problem sorting the multiple array :(

Array trying to sort: (7 arrays put into an array) and I want to sort on
[count] Descending.
This is displayed using print_r() function.

Array
(
[czero] => Array
(
[game_type] => czero
[game_label] => Condition Zero
[count] => 2
)
[cstrike] => Array
(
[game_type] => cstrike
[game_label] => Counter-Strike
[count] => 3
)
[dod] => Array
(
[game_type] => dod
[game_label] => Day of Defeat
[count] => 1
)
[valve] => Array
(
[game_type] => valve
[game_label] => Half Life
[count] => 0
)
[gearbox] => Array
(
[game_type] => gearbox
[game_label] => Opposing Force
[count] => 0
)
[ricochet] => Array
(
[game_type] => ricochet
[game_label] => Ricochet
[count] => 0
)
[tfc] => Array
(
[game_type] => tfc
[game_label] => Team Fortress Classic
[count] => 1
)
)

Does anyone know how to do this ?
I have tried various array sorting functions without success :(

If you require more information, then let me know.

Thanks in advance
Paul Kirby

Jul 17 '05 #3
Thanks to both of you that helped a little, had to alter bits of their
examples to fit my needs :)

Paul Kirby

"Virgil Green" <vj*@DESPAMobsy dian.com> wrote in message
news:C2******** *****@newssvr23 .news.prodigy.c om...
"Paul Kirby" <ad***@initcorp .co.uk> wrote in message
news:ca******** ***********@new s.demon.co.uk.. .
Hello All

I am trying to update me code to use arrays to store a group of

information
and I have come up with a problem sorting the multiple array :(

Array trying to sort: (7 arrays put into an array) and I want to sort on
[count] Descending.
This is displayed using print_r() function.

Array
(
[czero] => Array
(
[game_type] => czero
[game_label] => Condition Zero
[count] => 2
)
[cstrike] => Array
(
[game_type] => cstrike
[game_label] => Counter-Strike
[count] => 3
)
[dod] => Array
(
[game_type] => dod
[game_label] => Day of Defeat
[count] => 1
)
[valve] => Array
(
[game_type] => valve
[game_label] => Half Life
[count] => 0
)
[gearbox] => Array
(
[game_type] => gearbox
[game_label] => Opposing Force
[count] => 0
)
[ricochet] => Array
(
[game_type] => ricochet
[game_label] => Ricochet
[count] => 0
)
[tfc] => Array
(
[game_type] => tfc
[game_label] => Team Fortress Classic
[count] => 1
)
)

Does anyone know how to do this ?
I have tried various array sorting functions without success :(


See www.php.net/uasort and associated functions.

- Virgil

Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
2095
by: Alan Searle | last post by:
I have been trying to get an array to sort on a particular column and have been having problems with my main key ... array_multisort($dirlist, SORT_DESC, SORT_STRING); This function takes an array ($dirlist) which has been loaded with a directory listing where each directory has its own index number ("dirid") and displays the listing in descending order by name ("dirname"). This works fine for the sorting of the names but the index...
5
1811
by: Eclectic | last post by:
Hey all, I have been using usort to sort my multi dimensional arrays ... function cmp($a, $b){ if($a == $b){ return 0; } return ($a < $b) ? -1 : 1; }
1
1454
by: Jochen Califice | last post by:
hello ng! got a problem sorting my multi-dimensional array. I have the following situation of an array: $fahrzeugarray= "23" $fahrzeugarray="Audi TT" $fahrzeugarray= "34" $fahrzeugarray="VW Polo 1.2" $fahrzeugarray= "57"
7
3051
by: Karin Jensen | last post by:
Hi I am running a PHP program that connects to an Access 2000 database via ODBC: $results = odbc_exec($connection_id, $sql_select); Is it possible to sort the contents of $results? I wish to use a regex-based sort order that you can't do in Access 2000* SQL with "ORDER BY".
2
2119
by: lgbjr | last post by:
Hi All, Is there a way to sort multiple dimensions of an array in VB.NET? Let's say I have the following in a multi-dimension array: 3081 100 2 3081 100 1 3081 20 1 3081 1 2 3021 100 2
0
3246
by: Brian Henry | last post by:
Here is another virtual mode example for the .NET 2.0 framework while working with the list view. Since you can not access the items collection of the list view you need to do sorting another way... here is my code on how I did it to help anyone starting out get an idea of how to use virtual mode in ..NET 2.0 Imports CrystalDecisions.CrystalReports.Engine Imports CrystalDecisions.Shared
1
9004
by: Christopher Miles | last post by:
Hello, I am trying to sort this array by the time field , and have all the other fields move accordingly to match .. Group_Array(0) - name Group_Array(1) - severity Group_Array(2) - Time Group_Array(3) - Tier Group_Array(4) - Description
5
5271
by: JC | last post by:
Hi all, I have scoured the internet for articles on sorting multi-dimensional arrays of generic types e.g. T using the IComparer<Tinterface and have run into a real roadblack. There does not seem to be an easy way to do this. I have found implementations that use the non-generic IComparer interface but it seems to me to be more elegant to use generics if possible. Has anyone else had this problem and could they suggest or poin me
5
4966
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums there are, it crashes. There are currently 6 columns, and I only want 4. How do I remove the last two (discount and date)? Here is a link: http://www.jaredmoore.com/tablesorter/docs/salestable.html Here is some jquery js that I think...
0
9840
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9682
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10565
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10623
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9397
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7000
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5668
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4474
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 we have to send another system
3
3124
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.