Current location: Hot Scripts Forums » Programming Languages » PHP » [SOLVED] Sorting multi array on key


[SOLVED] Sorting multi array on key

Reply
  #1 (permalink)  
Old 03-25-08, 11:47 PM
patter patter is offline
Newbie Coder
 
Join Date: Jul 2006
Posts: 43
Thanks: 0
Thanked 0 Times in 0 Posts
[SOLVED] Sorting multi array on key

I have an array that looks something like this
PHP Code:

 Array

(
    [
title] => Array
        (
            [
0] => New title
            
[5] => Default title
            
[3] => Old Title
        
)
    [
addr] => Array
        (
            [
0] => New title
            
[5] => Default title
            
[3] => Old Title
        
)

I need to have it sorted on the keys of the inner arrays so the result is
PHP Code:

Array

(
    [
title] => Array
        (
            [
0] => New title
            
[3] => Old Title
            
[5] => Default title
        
)
    [
addr] => Array
        (
            [
0] => New title
            
[3] => Old Title
            
[5] => Default title
        
)
)[/ 
I tried using this code but it doesn't work.
PHP Code:

 function MultiKeySort($k)

 {
   if (! 
is_array($k))
    
$k = array();
    
   foreach (
$k as $key)
   {
     
ksort($key);
   } 
   return 
$k;
 }
$myArry MultiKeySort($myArray); 
Would someone please explain how to get this sorted correctly?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 03-26-08, 01:01 AM
phpdoctor's Avatar
phpdoctor phpdoctor is offline
Code Guru
 
Join Date: Feb 2007
Location: New Zealand
Posts: 767
Thanks: 4
Thanked 2 Times in 2 Posts
You need to change the array and the foreach loop in the function.
Try this:
PHP Code:



function MultiKeySort($k
 { 
   if (! 
is_array($k)) 
    
$k = array(); 
     
   foreach (
$k as $key => $val
   { 
     
ksort($val); 
     
$k[$key] = $val;
   }  
   return 
$k
 } 
$myArry MultiKeySort($myArray); 
Hope that helps,
Lex
__________________
01010000 01001000 01010000
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 03-26-08, 01:59 AM
Jay6390's Avatar
Jay6390 Jay6390 is offline
Code Master
 
Join Date: Apr 2007
Location: United Kingdom
Posts: 1,330
Thanks: 0
Thanked 0 Times in 0 Posts
PHP Code:

function MultiKeySort(&$arr)
{
    foreach(
$arr as $k=>&$v)
    {
        if(
is_array($v))
        {
            
MultiKeySort($v);
        }else{
            
ksort($arr);
            break;
        }
    }

I was going to suggest this. It works just as expected, although I'm not 100% that it is perfect

Jay
__________________
Useful Tutorials
[ PHP Video-1-2-3 ] [ MySQL 1-2-3 ]
For any php function reference type

www.php.net/FunctionName
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 03-26-08, 08:10 AM
patter patter is offline
Newbie Coder
 
Join Date: Jul 2006
Posts: 43
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by phpdoctor View Post
You need to change the array and the foreach loop in the function.
Try this:

Hope that helps,
Lex
Thank you very much. It works great.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 03-26-08, 04:43 PM
phpdoctor's Avatar
phpdoctor phpdoctor is offline
Code Guru
 
Join Date: Feb 2007
Location: New Zealand
Posts: 767
Thanks: 4
Thanked 2 Times in 2 Posts
No problem, not sure if you wanted a recursive function like Jay made... but my one will work for your array.
__________________
01010000 01001000 01010000
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 03-27-08, 08:52 AM
patter patter is offline
Newbie Coder
 
Join Date: Jul 2006
Posts: 43
Thanks: 0
Thanked 0 Times in 0 Posts
I actually didn't notice the difference. I thought he had just quoted your code and was commenting on it. Thanks for the code Jay.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 03-27-08, 09:00 AM
Jay6390's Avatar
Jay6390 Jay6390 is offline
Code Master
 
Join Date: Apr 2007
Location: United Kingdom
Posts: 1,330
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by patter View Post
I actually didn't notice the difference. I thought he had just quoted your code and was commenting on it. Thanks for the code Jay.
The difference is that my code will sort the top most array for each array element, where as the one supplied by Lex is for a 'static' format array type. Also, mine is passed by reference rather than by value, therefore you are editing the array directly, rather than having to return an array, so simply using
PHP Code:

MultiKeySort($myArry); 

will sort it (Note there is no '$myArry=' before it). Either way, as long as one works
__________________
Useful Tutorials
[ PHP Video-1-2-3 ] [ MySQL 1-2-3 ]
For any php function reference type

www.php.net/FunctionName

Last edited by Jay6390; 03-27-08 at 09:03 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #8 (permalink)  
Old 03-27-08, 03:18 PM
phpdoctor's Avatar
phpdoctor phpdoctor is offline
Code Guru
 
Join Date: Feb 2007
Location: New Zealand
Posts: 767
Thanks: 4
Thanked 2 Times in 2 Posts
Wow another good idea Jay, I always forget about the referencing symbol.
I use that technique alot these days Thanks.
__________________
01010000 01001000 01010000
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #9 (permalink)  
Old 03-27-08, 04:23 PM
Jay6390's Avatar
Jay6390 Jay6390 is offline
Code Master
 
Join Date: Apr 2007
Location: United Kingdom
Posts: 1,330
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by phpdoctor View Post
Wow another good idea Jay, I always forget about the referencing symbol.
I use that technique alot these days Thanks.
Lol, I only thought about it cos i was too lazy to code it another way
__________________
Useful Tutorials
[ PHP Video-1-2-3 ] [ MySQL 1-2-3 ]
For any php function reference type

www.php.net/FunctionName
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Sorting an multi-diminsional array... Dan Man PHP 5 03-10-08 06:51 PM
Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')' Dr. Forensics PHP 3 07-15-06 04:54 PM
Sorting Array v1brazy PHP 2 12-20-05 11:46 AM
Sorting a massive multidimensional array dave111 PHP 1 12-12-05 05:09 PM
linking to iframe not working :( j0d JavaScript 5 01-19-04 09:14 PM


All times are GMT -5. The time now is 07:10 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.