Current location: Hot Scripts Forums » Programming Languages » PHP » Sorting an multi-diminsional array...


Sorting an multi-diminsional array...

Reply
  #1 (permalink)  
Old 03-10-08, 03:47 PM
Dan Man Dan Man is offline
Newbie Coder
 
Join Date: Sep 2006
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Sorting an multi-diminsional array...

Hi,

I am trying to sort a multi-dimensional array.

The array is set up in such a way that the indexes are not consistent. For example:

PHP Code:

$the_array[2]["value"] = 3;

$the_array[1]["value"] = 7;
$the_array[4]["value"] = 1;
$the_array[10]["value"] = 10
I am trying to sort this array based on the value. I created a custom comparison function to use with usort(). It is below:

PHP Code:

function cmp($a$b)

{
    if(
$a['value'] == $b['value'])
        return 
0;

    return (
$a['venueValue'] > $b['venueValue']) ? -1;

Then in the code, I call:

PHP Code:

usort($the_array"cmp"); 

The usort function, when comparing, seems to want to loop through all possible indexes.

That is, it will compare $the_array[0]["value"] even though it does not exist there is no index 0.

This is the observation I have made.

Any ideas of how to modify the function, or use something besides usort to accomplish the sorting on only the available indexes?

Thanks in advance!
Reply With Quote
  #2 (permalink)  
Old 03-10-08, 04:15 PM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
How about:
PHP Code:

asort($the_array); 

Code:
Array
(
    [4] => Array
        (
            [value] => 1
        )

    [2] => Array
        (
            [value] => 3
        )

    [1] => Array
        (
            [value] => 7
        )

    [10] => Array
        (
            [value] => 10
        )

)
Reply With Quote
  #3 (permalink)  
Old 03-10-08, 04:28 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 Dan Man View Post
That is, it will compare $the_array[0]["value"] even though it does not exist there is no index 0.

This is the observation I have made.
The problem is not that it tries the array index 0, its that it returns the array sorted and doesnt keep the original keys. it returns the following
Code:
Array
(
    [0] => Array
        (
            [value] => 1
        )

    [1] => Array
        (
            [value] => 3
        )

    [2] => Array
        (
            [value] => 7
        )

    [3] => Array
        (
            [value] => 10
        )

)
As you can see, it just assigns new index values starting at 0

Use the code Nico provided instead
Jay
__________________
Useful Tutorials
[ PHP Video-1-2-3 ] [ MySQL 1-2-3 ]
For any php function reference type

www.php.net/FunctionName
Reply With Quote
  #4 (permalink)  
Old 03-10-08, 04:38 PM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
Yep, Jay got that right. And on a further note, you could also use your existing function, along with uasort(), to maintain the indexes. But I'd rather use asort() alone, unless you have some specific reason why you prefer doing it the other way.
Reply With Quote
  #5 (permalink)  
Old 03-10-08, 05:40 PM
Dan Man Dan Man is offline
Newbie Coder
 
Join Date: Sep 2006
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks Nico and Jay!

The uasort was exactly what I was looking for!

I forgot to mention that my array hold multiple keys, so I wasn't sure how asort would treat that.

That is:

PHP Code:

$the_array[2]["value"] = 3;
$the_array[2]["rank"] = 1;
$the_array[1]["value"] = 7;
$the_array[1]["rank"] = 3
etc. I am still curious how asort would treat this. Could you specify a key for it to sort by?

Thanks again!
Reply With Quote
  #6 (permalink)  
Old 03-10-08, 05:51 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
you mean something like
PHP Code:

<?php
$the_array
[2]["value"] = 3;
$the_array[2]["rank"] = 1;
$the_array[1]["value"] = 7;
$the_array[1]["rank"] = 3;

foreach(
$the_array as $k=>$v)
{
asort($the_array[$k]);
}
ksort($the_array);
echo 
"<pre>".print_r($the_array,true)."</pre>";
?>
gives the output
Code:
Array
(
    [1] => Array
        (
            [rank] => 3
            [value] => 7
        )

    [2] => Array
        (
            [rank] => 1
            [value] => 3
        )

)
If you want to sort the subkeys in the arrays by the actual keys rather than the values, replace asort with ksort for that also

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

www.php.net/FunctionName
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
Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')' Dr. Forensics PHP 3 07-15-06 03:54 PM
Sorting Array v1brazy PHP 2 12-20-05 10:46 AM
Sorting a massive multidimensional array dave111 PHP 1 12-12-05 04:09 PM
Serializing a set of arrays dannyallen PHP 2 10-11-04 03:04 AM
linking to iframe not working :( j0d JavaScript 5 01-19-04 08:14 PM


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