sorting multidimensional arrays issue

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
arden
Forum Newbie
Posts: 4
Joined: Thu Oct 03, 2013 8:53 am

sorting multidimensional arrays issue

Post by arden »

Hi,

I'm trying to sort small arrays based on a specified key value (link), eg

Code: Select all

Array
(
    [0] => Array
        (
            [id] => 273113
            [file] => /volume2/Games/Xbox360/ISO/Wolfenstein.iso
            [size] => 7838695424
            [fdate] => 1377249817
            [crc32] => 34AE1DE2
            [profile] => 5
            [link] => 
        )

    [1] => Array
        (
            [id] => 272424
            [file] => /volume2/Games/Xbox360/wolg.iso
            [size] => 42
            [fdate] => 1379667986
            [crc32] => 34AE1DE2
            [profile] => 5
            [link] => /volume2/Games/Xbox360/ISO/Wolfenstein.iso
        )

    [2] => Array
        (
            [id] => 272422
            [file] => /volume2/Games/Xbox360/nesttest.iso
            [size] => 31
            [fdate] => 1379923105
            [crc32] => 34AE1DE2
            [profile] => 5
            [link] => /volume2/Games/Xbox360/wolg.iso
        )

)
I'd like to soft the array by the link value so key 0 (which has a blank value) will always be the first in the array, and the other 2 with entry's can be in what ever order they like after.

I've tried messing about with a number of array sorting functions but can't seem to figure it out, any pointers would be helpful! (ps. I know the above array is already sorted but its just an example)
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: sorting multidimensional arrays issue

Post by Celauran »

A helper I wrote to do just that.

Code: Select all

/**
 * Sorts a multidimensional array by the specified key
 *
 * @param  array  $array     Array to be sorted
 * @param  string $sortOn    Array key on which to sort
 * @param  const  $direction Direction of sort (SORT_ASC, SORT_DESC)
 * @return array             Array sorted by $sortOn
 */
public static function sortByValue($array, $sortOn, $direction = SORT_DESC) {
	$sort = array();
	foreach ($array as $key => $value) {
		$sort[$key] = $value[$sortOn];
	}

	array_multisort($sort, $direction, $array);

	return $array;
}
arden
Forum Newbie
Posts: 4
Joined: Thu Oct 03, 2013 8:53 am

Re: sorting multidimensional arrays issue

Post by arden »

Celauran wrote:A helper I wrote to do just that.
Super, thanks that did the trick!

Thanks again!
arden
Forum Newbie
Posts: 4
Joined: Thu Oct 03, 2013 8:53 am

Re: sorting multidimensional arrays issue

Post by arden »

Might it be possible to sort on file as a secondary sort after the sort on link has been done?

I tried calling the function before the sort on link but even if link is blank on all elements it still overrides the sorting on file and messes up the order.
arden
Forum Newbie
Posts: 4
Joined: Thu Oct 03, 2013 8:53 am

Re: sorting multidimensional arrays issue

Post by arden »

Think I got it, although limited testing, if someone knows better please do let me know, thanks!

Code: Select all

function sortByValue($array, $sortOn, $sortOn2, $direction = SORT_ASC) 
{
    $sort   = array();
    $sort2 = array();
	
    foreach ($array as $key => $value) 	{
        $sort[$key]  = $value[$sortOn];
        $sort2[$key] = $value[$sortOn2];
    }

    array_multisort($sort, $direction, $sort2, $direction, $array);

    return $array;
}
Post Reply