简体   繁体   中英

Sort Multi Dimensional Array by its keys from another arrays values

This is an issue I haven't come across before, and there doesn't appear to be a function for this.

I'm trying to sort the following multi dimensional array by its keys

Array (
    [hiphop] => Array (
        [0] => 2123
        [1] => 5683
        [2] => 2345
        [3] => 4567
    )
    [rnb] => Array (
        [0] => 2123
        [1] => 5683
        [2] => 2345
        [3] => 4567
    )
    [dubstep] => Array ( )
    [reggae] => Array ( )
    [trap] => Array ( )
)

From this arrays values

Array (
    [0] => hiphop
    [1] => dubstep
    [2] => reggae
    [3] => trap
    [4] => rnb
    [5] => rnb
) 

Has anyone attempted this before or know a workaround?

An explanation would be great as I'm new to multi dimensional arrays

Many thanks in advance if you can help!

The final output would match the value organsiation from the non multi dimensional array like so

Array (
    [hiphop] => Array (
        [0] => 2123
        [1] => 5683
        [2] => 2345
        [3] => 4567
    )
    [dubstep] => Array ( )
    [reggae] => Array ( )
    [trap] => Array ( )
    [rnb] => Array (
        [0] => 2123
        [1] => 5683
        [2] => 2345
        [3] => 4567
    )
)

You can use uksort for that, which lets you sort by key, based on a user supplied comparison function. Using that function, you can make a function yourself, so you can hide the complex functionality in an easy one.

For instance, you want a function like ksort , only you want to specify an external array of keys, so such a function could look like this:

bool ksort_ext(&$array, $keys)

For the implementation, you can wrap uksort, like so:

function ksort_ext(&$array, $keys)
{
    return uksort($array, function($a, $b) use ($keys) {
            // Result of callback is based on the position of $a and $b in $keys
            return array_search($a, $keys) - array_search($b, $keys);
        });
}

Then, you can call it like you would call other sort functions, only with the external array of keys as a second parameter.

$array1 = array(
    'hiphop' => array(1,2,3),
    'rnb' => array(1,2,3),
    'dubstep' => array(1,2,3),
    'reggae' => array(1,2,3),
    'trap' => array(1,2,3));

$array2 = array('hiphop', 'dubstep', 'reggae', 'trap', 'rnb');

var_dump(ksort_ext($array1, $array2)); // Should return true
var_dump($array1); // Should display the ordered array,

The easiest Way would be to "flip" the array definining the sorting - then grab the values matching the new keys in the other array:

$s = array ("hiphop","dubstep","reggae","trap","rnb");
$target = array_flip($s);

foreach($target AS $key => &$value){
  $value = $array_containing_unsorted_values[$key];
}

Note: does not work if the same value appears twice in the sorting array - but that never should happen, cause does not make sence for a sorting-definition .

Should be way faster than using array_search 2 times within each sorting comparission.

result:

Array
(
    [hiphop] => Array
        (
            [0] => 2123
            [1] => 5683
            [2] => 2345
            [3] => 4567
        )

    [dubstep] => Array
        (
        )

    [reggae] => Array
        (
        )

    [trap] => Array
        (
        )

    [rnb] => Array
        (
            [0] => 2123
            [1] => 5683
            [2] => 2345
            [3] => 4567
        )

)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM