Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.