Jump to content

Sort Problem


heffe6

Recommended Posts

Hello everyone,

 

Im trying to sort this array:

 

Array

(

    [35] => Array

            (

                  [first_name] => Dave

                  [last_name] => Park

                  [cost] => 35

                  [amt] => 3

            )

 

      [46] => Array

            (

                  [first_name] => Steven

                  [last_name] => Lewis

                  [cost] => 60

                  [amt] => 6

            )

...  etc

 

I'd like to sort the first level array by the value in [last_name], then by the value [first_name], and maintain all keys. Can anyone help?

 

Link to comment
https://forums.phpfreaks.com/topic/188301-sort-problem/
Share on other sites

Thanks for your help,

 

this code seems to work:

 

function sort($a, $b) {

        if ($a['list'] == $b['list']) {

            return 0;

        }

        return ($a['list'] < $b['list']) ? -1 : 1; 

}

 

Then i call uasort($d, 'sort');

 

When I output my array, the sort works. But I get the following warnings:

 

Warning (2): sort() expects parameter 2 to be long, array given [APP\models\timesheet.php, line 103]

 

Warning (2): uasort() [function.uasort]: Array was modified by the user comparison function [APP\models\timesheet.php, line 103]

 

Any ideas? 

 

 

Link to comment
https://forums.phpfreaks.com/topic/188301-sort-problem/#findComment-994082
Share on other sites

Try this function

 

function sort_array($array, $key, $order)
{
if ($order=="DESC")
	$or="arsort";
else
	$or="asort";
for ($i = 0; $i < sizeof($array); $i++) 
{
	$sort_values[$i] = $array[$i][$key];
} 
$or($sort_values);
reset ($sort_values);
while (list ($arr_key, $arr_val) = each ($sort_values)) 
{
	$sorted_arr[] = $array[$arr_key];
}
return $sorted_arr;
}

 

Link to comment
https://forums.phpfreaks.com/topic/188301-sort-problem/#findComment-994083
Share on other sites

Thanks for your help,

 

this code seems to work:

 

function sort($a, $b) {

        if ($a['list'] == $b['list']) {

            return 0;

        }

        return ($a['list'] < $b['list']) ? -1 : 1; 

}

 

Then i call uasort($d, 'sort');

 

When I output my array, the sort works. But I get the following warnings:

 

Warning (2): sort() expects parameter 2 to be long, array given [APP\models\timesheet.php, line 103]

 

Warning (2): uasort() [function.uasort]: Array was modified by the user comparison function [APP\models\timesheet.php, line 103]

 

Any ideas?

 

try using another name for your function than "sort"

sort() also is a PHP fnc

maybe that causes the error...

apart from that, your code seems fine to me

Link to comment
https://forums.phpfreaks.com/topic/188301-sort-problem/#findComment-994091
Share on other sites

Thanks for your help everyone. My problem was I was calling this from within a class, (I failed to mention that) and I was having trouble accessing my custom sorting function. 

 

Another look through the manual, and I figured I had to do this:

 

uasort($arr, array("className", "functionName"));

 

 

Link to comment
https://forums.phpfreaks.com/topic/188301-sort-problem/#findComment-994306
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.