sorting an array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

sorting an array

Post by malcolmboston »

ok i have an array like this:

Code: Select all

$array['unique'][1] = 2;
$array['unique'][2] = 7;
$array['unique'][3] = 4;
$array['unique'][4] = 5;
$array['unique'][5] = 6;
$array['unique'][6] = 8;
and i want tor reorder the array so it goes DESC on the value so it would go

Code: Select all

$array['unique'][6] = 8;
$array['unique'][2] = 7;
$array['unique'][5] = 6;
$array['unique'][4] = 5;
$array['unique'][3] = 4;
$array['unique'][1] = 2;
how can this be achieved?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Code: Select all

sort($array['unique']);
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

what if you have an array in this format?

Code: Select all

Array
(
    [1] => Array
        (
            [date] => 1
            [pageLoads] => 98
            [unique] => 9
            [firstTime] => 5
            [returning] => 4
        )

    [2] => Array
        (
            [date] => 2
            [pageLoads] => 170
            [unique] => 72
            [firstTime] => 66
            [returning] => 6
        )

    [3] => Array
        (
            [date] => 3
            [pageLoads] => 448
            [unique] => 100
            [firstTime] => 89
            [returning] => 11
        )

    [4] => Array
        (
            [date] => 4
            [pageLoads] => 472
            [unique] => 179
            [firstTime] => 173
            [returning] => 6
        )
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

depends on how you want this array sorted.
But probably http://de2.php.net/usort
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

well i would like them ordered in the ordered of a certain value for eg DESC unique

see my first post for an example

sorting arrays has always been a pain in the ****
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

yo, usort.
Write a function that compares two arrays by the value of the element unique.
There are examples at http://de2.php.net/usort
printf
Forum Contributor
Posts: 173
Joined: Wed Jan 12, 2005 5:24 pm

Post by printf »

array_multisort();

Code: Select all

<?

$array = array
(
    1 => array
        (
            'date' => 1,
            'pageLoads' => 98,
            'unique' => 9,
            'firstTime' => 5,
            'returning' => 4
        ),

    2 => array
        (
            'date' => 2,
            'pageLoads' => 170,
            'unique' => 72,
            'firstTime' => 66,
            'returning' => 6
        ),

    3 => array
        (
            'date' => 3,
            'pageLoads' => 448,
            'unique' => 100,
            'firstTime' => 89,
            'returning' => 11
        ),

    4 => array
        (
            'date' => 4,
            'pageLoads' => 472,
            'unique' => 179,
            'firstTime' => 173,
            'returning' => 6
        ) 
);


// key to sort on...

$key = 'unique';

// type of sort (int, str)

$type = 'int';

// order of sort (asc, desc)

$order = 'desc';

// the array of key values to sort on

$sort = array ();

foreach ( $array AS $v )
{
	$sort[] = $v[$key];
}

$type  = ( $type == 'int' ? SORT_NUMERIC : SORT_STRING );
$order = ( $order == 'asc' ? SORT_ASC : SORT_DESC );

array_multisort ( $sort, $order, $type, $array );

print_r ( $array );

?>

pif!
Post Reply