PHP Coding
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
IBM developerWorks
Go Back   Codewalkers ForumsPHP RelatedPHP Coding

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
You eat, breathe and sleep innovation. Build your mobile intelligence with BlackBerry® experts this July. Register Today!
  #1  
Old September 25th, 2002, 05:01 PM
Nimco Nimco is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 132 Nimco User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via AIM to Nimco
Sorting Arrays

OK, here is the scenario: I have an array as described below. How do I sort it based on the value in the $array[][title] field by alphabetical order?

php Code:
Original - php Code
  1.  
  2. (
  3.     [0] => Array
  4.         (
  5.             [title] => ghi
  6.             [value] => testing3
  7.         )
  8.  
  9.     [1] => Array
  10.         (
  11.             [title] => abc
  12.             [value] => testing1
  13.         )
  14.  
  15.     [2] => Array
  16.         (
  17.             [title] => jkl
  18.             [value] => testing4
  19.         )
  20.  
  21.     [3] => Array
  22.         (
  23.             [title] => def
  24.             [value] => testing2
  25.         )
  26.  
  27. )

Reply With Quote
  #2  
Old September 25th, 2002, 05:20 PM
notepad notepad is offline
Codewalkers Loyal (3000 - 3499 posts)
 
Join Date: Apr 2007
Location: Central, IL USA
Posts: 3,214 notepad User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Send a message via AIM to notepad
RE: Sorting Arrays

i think something along these lines would work.. probably an easier way.
php Code:
Original - php Code
  1.  
  2. <?php
  3.  
  4. for($i=0; $i<count($myArray); $i++)
  5. {
  6.     $old[] = $myArray[$i]['title'];
  7. }
  8. sort($old);
  9.  
  10. foreach($old as $key=>$value)
  11. {
  12.     for($n=0; $n<count($myArray); $n++)
  13.     {
  14.         if($old[$key] == $myArray[$n]['title'])
  15.         {
  16.             print("title: {$myArray[$n]['title']}<br>n");
  17.             print("value: {$myArray[$n]['value']}<br><br>n");
  18.         }
  19.     }
  20. }
  21.  
  22. ?>

Reply With Quote
  #3  
Old September 25th, 2002, 05:37 PM
Matt Matt is offline
Moderator
Codewalkers Specialist (4000 - 4499 posts)
 
Join Date: Apr 2007
Location: Florida
Posts: 4,158 Matt User rank is Private First Class (20 - 50 Reputation Level)Matt User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 h 10 m 20 sec
Reputation Power: 6
RE: Sorting Arrays

Try this out...

php Code:
Original - php Code
  1. <?
  2. $array[0] = array ("title" => "ghi", "value" => "testing3");
  3. $array[1] = array ("title" => "abc", "value" => "testing1");
  4. $array[2] = array ("title" => "jkl", "value" => "testing4");
  5. $array[3] = array ("title" => "def", "value" => "testing2");
  6.  
  7. print_r($array);
  8.  
  9. foreach($array as $value) {
  10.     $sortarray[]$value["title"];
  11. }
  12.  
  13. array_multisort($array, $sortarray);
  14.  
  15. print_r($array);
  16. ?>


Reply With Quote
  #4  
Old September 26th, 2002, 05:27 AM
Nimco Nimco is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 132 Nimco User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via AIM to Nimco
RE: Sorting Arrays

Cheers Matt, it works fine.

Reply With Quote
Reply

Viewing: Codewalkers ForumsPHP RelatedPHP Coding > Sorting Arrays


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 

IBM developerWorks




© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway