Page 1 of 1

sorting multidimensional arrays issue

Posted: Thu Oct 03, 2013 9:02 am
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)

Re: sorting multidimensional arrays issue

Posted: Thu Oct 03, 2013 9:43 am
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;
}

Re: sorting multidimensional arrays issue

Posted: Fri Oct 04, 2013 2:04 am
by arden
Celauran wrote:A helper I wrote to do just that.
Super, thanks that did the trick!

Thanks again!

Re: sorting multidimensional arrays issue

Posted: Mon Oct 07, 2013 2:43 am
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.

Re: sorting multidimensional arrays issue

Posted: Mon Oct 07, 2013 2:50 am
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;
}