Page 1 of 1

Sorting a Multi-Dimensional Array: Reversing the Order

Posted: Tue Jun 15, 2010 7:20 pm
by lauthiamkok
Hi,

How can I sort a multi-dimensional array in a reverse order?

For instance,

Code: Select all

$songs =  array(
		'3' => array('artist'=>'The Smashing Pumpkins', 'songname'=>'Soma'),
		'4' => array('artist'=>'The Decemberists', 'songname'=>'The Island'),
		'1' => array('artist'=>'Fleetwood Mac', 'songname' =>'Second-hand News'),
		'2' => array('artist'=>'Jack Johnon', 'songname' =>'Only the Ocean')
	);
	
print_r($songs);
will result in this,

Code: Select all

Array
(
    [3] => Array
        (
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        )

    [4] => Array
        (
            [artist] => The Decemberists
            [songname] => The Island
        )

    [1] => Array
        (
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        )

    [2] => Array
        (
            [artist] => Jack Johnon
            [songname] => Only the Ocean
        )

)
but I would like to have it in this order instead,

Code: Select all

Array
(

   [2] => Array
        (
            [artist] => Jack Johnon
            [songname] => Only the Ocean
        )

 [1] => Array
        (
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        )

  [4] => Array
        (
            [artist] => The Decemberists
            [songname] => The Island
        )

    [3] => Array
        (
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        )

)
I want to keep the key values which are [2],[1],[4],[3] (the original order is [3],[4],[1],[2]).

do I need to write a function to do this?

Many thanks,
Lau

Re: Sorting a Multi-Dimensional Array: Reversing the Order

Posted: Tue Jun 15, 2010 7:34 pm
by requinix
If all you want is to reverse it then just reverse it. But if you want actual sorting then either use usort+a function or take a look at example #3 for array_multisort.

Re: Sorting a Multi-Dimensional Array: Reversing the Order

Posted: Tue Jun 15, 2010 7:53 pm
by lauthiamkok
tasairis wrote:If all you want is to reverse it then just reverse it. But if you want actual sorting then either use usort+a function or take a look at example #3 for array_multisort.
thanks I have tried the array_reverse - it works but it will replace the original array keys to 0, 1, 2, 3 which is not I want....


here is the output,

Code: Select all

Array
(
    [0] => Array
        (
            [artist] => Jack Johnon
            [songname] => Only the Ocean
        )

    [1] => Array
        (
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
        )

    [2] => Array
        (
            [artist] => The Decemberists
            [songname] => The Island
        )

    [3] => Array
        (
            [artist] => The Smashing Pumpkins
            [songname] => Soma
        )

)
I think I should sort it by date, the array should have been like this,

Code: Select all

Array
(
    [3] => Array
        (
            [artist] => The Smashing Pumpkins
            [songname] => Soma
            [date] => 1276646720
        )

    [4] => Array
        (
            [artist] => The Decemberists
            [songname] => The Island
            [date] => 1276646724
        )

    [1] => Array
        (
            [artist] => Fleetwood Mac
            [songname] => Second-hand News
            [date] => 1276646728
        )

    [2] => Array
        (
            [artist] => Jack Johnson
            [songname] => Only the Ocean
            [date] => 1276646731
        )

)
thanks.

Re: Sorting a Multi-Dimensional Array: Reversing the Order

Posted: Tue Jun 15, 2010 9:46 pm
by McInfo
In PHP, arrays have an "internal pointer" that points to the current array element, like a bookmark points to a page in a book. Don't confuse this pointer with the keys of an array. As a user of PHP, the internal pointer is invisible to you, but you can ask PHP about the element the pointer is currently pointing to or point it to a different element. You do this by using a group of array pointer functions:
  • current()
  • each()
  • end()
  • key()
  • next()
  • prev()
  • reset()
To reverse your array and preserve the keys, first move the internal pointer to the end of the array. Then, looping backwards through the array, append the elements to a new array.

PHP Manual: Array Functions

Re: Sorting a Multi-Dimensional Array: Reversing the Order

Posted: Wed Jun 16, 2010 12:30 am
by requinix
lauthiamkok wrote:thanks I have tried the array_reverse - it works but it will replace the original array keys to 0, 1, 2, 3 which is not I want....
Spoiler alert:

I link people to the PHP manual because they're supposed to read it, not just look at the pretty blue color and move on.

Re: Sorting a Multi-Dimensional Array: Reversing the Order

Posted: Wed Jun 16, 2010 7:57 am
by lauthiamkok
I got it sorted at last! thanks guys. :D

Code: Select all

function arsort_subvalue($a, $subkey) 
{
	#$a is the primary array, $k is key of the second array, $v is the array in $k.
	foreach($a as $k => $v) {
		
		#loop the new array with the subkey only and put them in a new array - $b.
		$b[$k] = $v[$subkey];
	}
	
	#sort the array in reverse order and maintain index association.
	arsort($b);
	
	#loop the $b array.
	foreach($b as $key => $val) {
		
		#put the key of the second array back on. 
		$c[$key] = $a[$key];
	}
	
	#return the new array - $c.
	return $c;
}

$songs =  array(
		'3' => array('artist'=>'The Smashing Pumpkins', 'songname'=>'Soma', 'date' =>1276646720),
		'4' => array('artist'=>'The Decemberists', 'songname'=>'The Island','date' =>1276646724),
		'1' => array('artist'=>'Fleetwood Mac', 'songname' =>'Second-hand News','date' =>1276646728),
		'2' => array('artist'=>'Jack Johnon', 'songname' =>'Only the Ocean','date' =>1276646731)
	);
	
$songs = arsort_subvalue($songs,'date');

Re: Sorting a Multi-Dimensional Array: Reversing the Order

Posted: Wed Jun 16, 2010 2:36 pm
by McInfo
That seems like a convoluted way to reverse an array. Maybe I don't really understand what your task is.

Is there something about the solution that tasairis alluded to that doesn't match your objective?

Code: Select all

$array = array_reverse($array, true);

Re: Sorting a Multi-Dimensional Array: Reversing the Order

Posted: Wed Jun 16, 2010 2:47 pm
by lauthiamkok
McInfo wrote:That seems like a convoluted way to reverse an array. Maybe I don't really understand what your task is.

Is there something about the solution that tasairis alluded to that doesn't match your objective?

Code: Select all

$array = array_reverse($array, true);
many thanks for this reply. the result of this line

Code: Select all

$array = array_reverse($array, true);
is the same as the function that I created actually.

this is what I am looking for actually.

I didn't realise that there is a difference between,

Code: Select all

$array = array_reverse($array, true);
and

Code: Select all

$array = array_reverse($array);
thank you so much! :D