devdarsh
devdarsh

Reputation: 95

How to sort multi-dimensional array in custom way

I am trying to re-arrange a multi-dimensional array based on custom arranged key as I am using jQuery sortable plugin. I read usort, ksort and other but those are sorting in descending or ascending order but on custom arranged key. I read uasort bu I don't understand how to implement with this.

 $original_array = 
  array(      
    'one' => array
        (
            'url' => 'home.php',
            'title' => 'Home',
            'permission' => array
                    (
                        'administrator' => yes,
                        'manager' => yes
                    )
        ),
    'two' => array
        (
            'url' => 'entries.php',
            'title' => 'Entries',
            'permission' => array
                (
                    'administrator' => yes,
                    'manager' => yes,
                )

        ),
      'three' => array
        (
            'url' => 'stock.php',
            'title' => 'Stock',
            'permission' => array
                (
                    'administrator' => 'yes',
                    'manager' => 'yes',
                )
        ),
    'four' => array
        (
            'url' => 'products.php',
            'title' => 'Products',
            'permission' => array
                (
                    'administrator' => yes,
                    'manager' => yes,
                )
        ),
      'five' => array
        (
            'url' => 'prices.php',
            'title' => 'Prices',
            'permission' => array
                (
                    'administrator' => yes,
                    'manager' => yes,
                )
        ),
    );

array to be arranged like in this order

$custom_array_to_be_arranged = 
  array(
      'three' => array
        (
            'url' => 'stock.php',
            'title' => 'Stock',
            'permission' => array
                (
                    'administrator' => 'yes',
                    'manager' => 'yes',
                )
        ),
    'one' => array
        (
            'url' => 'home.php',
            'title' => 'Home',
            'permission' => array
                    (
                        'administrator' => yes,
                        'manager' => yes
                    )
        ),      
    'five' => array
        (
            'url' => 'prices.php',
            'title' => 'Prices',
            'permission' => array
                (
                    'administrator' => yes,
                    'manager' => yes,
                )
        ),
    'two' => array
        (
            'url' => 'entries.php',
            'title' => 'Entries',
            'permission' => array
                (
                    'administrator' => yes,
                    'manager' => yes,
                )

        ),
    'four' => array
        (
            'url' => 'products.php',
            'title' => 'Products',
            'permission' => array
                (
                    'administrator' => yes,
                    'manager' => yes,
                )
        )
    );

Any help would be appreciated. Thanks.

Upvotes: 0

Views: 42

Answers (2)

Don't Panic
Don't Panic

Reputation: 41820

If you have an array of keys for a custom sort order, there are various ways to apply this to the array you want to sort.

One way to do this, rather than using a sort function, is to create a new sorted array by looping over the sort order array and pulling corresponding values from your original array.

$custom_keys = array('three', 'one', 'five', 'two', 'four');

foreach ($custom_keys as $key) {
    if (isset($original_array[$key])) {
        $sorted[$key] = $original_array[$key];      
    }
}

If you do want to use a sort function rather than creating a new array, use uksort, and use the custom sort array in your comparison callback. You can use array_search to find the correct order for the keys.

$custom_keys = array('three', 'one', 'five', 'two', 'four');

uksort($original_array, function($a, $b) use ($custom_keys) {
    $apos = array_search($a, $custom_keys);
    $bpos = array_search($b, $custom_keys);
    if ($apos < $bpos) return -1;
    if ($apos > $bpos) return 1;
    return 0;
});

Upvotes: 2

M. Eriksson
M. Eriksson

Reputation: 13635

If it can be totally random (or rather predefined according to a list), I guess you can create a custom function for it.

function customSort(array $newOrder, array $currentArray)
{
    $new = [];
    foreach($newOrder as $key) {
        if (array_key_exists($key, $currentArray)) {
            $new[$key] = $currentArray[$key];
        }
    }

    return $new;
}

Usage:

$newOrder     = ['three', 'one', .... ];
$currentArray = ['one' => ..., 'two' => ...];

$newSortedArray = customSort($newOrder, $currentArray);

This way you get a new array, sorted in the same order as the $newOrder list.

Upvotes: 1

Related Questions