1

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
    )
)
6
  • Please post another array of what you want the desired output to be because I cannot fully understand your explanation. Commented Oct 7, 2014 at 17:02
  • Why do you have two times 'rnb'? Commented Oct 7, 2014 at 17:03
  • Hard to explain @GolezTrol but it is needed Commented Oct 7, 2014 at 17:07
  • @NoahMatisoff That's now edited Commented Oct 7, 2014 at 17:07
  • what is the problem actually ? Commented Oct 7, 2014 at 17:09

2 Answers 2

2

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
        )

)
Sign up to request clarification or add additional context in comments.

1 Comment

+1 I was quite happy with my answer until I saw yours. Very clean, and for large arrays yours will perform better as well. I especially like the reference use of $value in the loop. If it was for me, I'd choose this as the accepted answer.
2

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,

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.