Jump to content

Sorting a 2D array


gregm

Recommended Posts

From from a earlier post I'm now able to print out my array fine, but now i would like to sort it. I've looked at asort and ksort and but am even more confused. I dont even know what to start coding.  The examples I've all seen are from simple arrays which I more or less understand.

 

I have this:

 

echo '<pre>' . print_r($myrecord, true) . '</pre>';

 

[11] => Array
        (
            [0] => PIE
            [1] => 18_disabled
            [2] => Distrib - A
        )

    [12] => Array
        (
            [0] => QUE
            [1] => 19_disabled
            [2] => Distrib - B
        )

    [13] => Array
        (
            [0] => CPK
            [1] => 2_disabled
            [2] => Distrib - DD
        )

    [14] => Array
        (
            [0] => MAL
            [1] => 20_disabled
            [2] => Distrib - N
        )

    [15] => Array
        (
            [0] => SWA
            [1] => 21_disabled
            [2] => Distrib - AN

 

What I would like to do is sort by [1] (The xx_disabled value) , so that I end up with:

 

[11] => Array
        (
            [0] => CPK
            [1] => 2_disabled
            [2] => Distrib - DD
        )

[12] => Array
        (
            [0] => PIE
            [1] => 18_disabled
            [2] => Distrib - A
        )

    [13] => Array
        (
            [0] => QUE
            [1] => 19_disabled
            [2] => Distrib - B
        )

    [14] => Array
        (
            [0] => MAL
            [1] => 20_disabled
            [2] => Distrib - N
        )

    [15] => Array
        (
            [0] => SWA
            [1] => 21_disabled
            [2] => Distrib - AN

 

Is this even do-able?

 

 

Link to comment
Share on other sites

Is it doable? Of course.

Will it have to be more than a simple array function? Probably.

 

Here's some code I roughed together; it could probably use some optimizing.

 

$newArray = array();

foreach($array as $value) { // $value is the nested array
$key = $value[1]; // This holds the value #_disabled 
$disabledInt = intval(preg_replace('(/*_disabled)', '', $key)); // ex. Turns the string 5_disabled to the int 5.
$newArray[$disabledInt] = $value; // We'll set the key of the array to the disabled integer.
}

// Now we can properly sort the array via disabled integer.
// Per asort, will be sorted lowest to highest integer.
asort($newArray);

// This line is optional.
// It will slow things down a bit (most array procedural functions are pretty slow), but it will essentially reset the keys to 0 to #, incrementing by one for each array element, as one would normally expect from an array.
$newArray = array_values($newArray);

 

The array I used to test with it:

$array = array(
0 => array
	(
		0 => 'test',
		1 => '5_disabled',
	),
1 => array
	(
		0 => 'test',
		1 => '8_disabled',
	),
2 => array
	(
		0 => 'test',
		1 => '2_disabled',
	),
3 => array
	(
		0 => 'test',
		1 => '1_disabled',
	),
4 => array
	(
		0 => 'test',
		1 => '7_disabled',
	),
);

Link to comment
Share on other sites

use a custom sort function to sort on the 2nd elements

 

<?php
$myrecords = array (
    11 => Array
        (
            0 => 'PIE' ,
            1 => '18_disabled',
            2 => 'Distrib - A'
        ),

    12 => Array
        (
            0 => 'QUE',
            1 => '19_disabled' ,
            2 => 'Distrib - B'
        ),

    13 => Array
        (
            0 => 'CPK',
            1 => '2_disabled' ,
            2 => 'Distrib - DD'
        )
);

function mysort($a, $b)
{
    return strnatcmp($a[1], $b[1]);
}

uasort($myrecords, 'mysort');

echo '<pre>'.print_r($myrecords, 1).'</pre>';
?>

 

results

Array
(
    [13] => Array
        (
            [0] => CPK
            [1] => 2_disabled
            [2] => Distrib - DD
        )

    [11] => Array
        (
            [0] => PIE
            [1] => 18_disabled
            [2] => Distrib - A
        )

    [12] => Array
        (
            [0] => QUE
            [1] => 19_disabled
            [2] => Distrib - B
        )

)

Link to comment
Share on other sites

Thanks for the code, but now i need to change the parameters a bit here - I need to sort by field [2] and then by field [1]. So i have this:

 

Basically sorted so that SiteA is first and then SiteB, and then within SiteA (and SiteB) the [1] with the lowest value comes first.

 

The more I try out my sorting routines the worse I am making things :(

 

Array
(
    [0] => Array
        (
            [0] => ServerA
            [1] => 26
            [2] => Site A
        )

    [1] => Array
        (
            [0] =>Server B
            [1] => 6
            [2] => Site A
        )

)
Array
(
    [0] => Array
        (
            [0] => Server B
            [1] => 0
            [2] => Site B
        )

    [1] => Array
        (
            [0] => ServerA
            [1] => 1
            [2] => Site B
        )

)

 

I would like it sorted to this:

 

Array
(
    [0] => Array
        (
            [0] =>Server B
            [1] => 6
            [2] => Site A
        )

    [1] => Array
        (
            [0] => ServerA
            [1] => 26
            [2] => Site A
        )



)
Array
(
    [0] => Array
        (
            [0] => Server B
            [1] => 0
            [2] => Site B
        )

    [1] => Array
        (
            [0] => ServerA
            [1] => 1
            [2] => Site B
        )

)

Link to comment
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.