0

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.

6
  • How do you want them arranged? The way you've shown looks almost random. Also, is jQuery relevant to the PHP code? Commented Jul 12, 2016 at 15:39
  • Hi, Yes according to $custom_array_to_be_arranged keys. I am forming $custom_array_to_be_arranged using jQuery sortable. Commented Jul 12, 2016 at 15:41
  • I am looking for the result as $custom_array_to_be_arranged Commented Jul 12, 2016 at 15:45
  • How do you get the new order it should be sorted by? An array with the the keys? Commented Jul 12, 2016 at 15:48
  • But why would it be sorted that way? Commented Jul 12, 2016 at 15:49

2 Answers 2

2

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;
});
Sign up to request clarification or add additional context in comments.

Comments

1

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.

2 Comments

Thanks @Magnus Eriksson It seems to be work. I will try your solution
did not tried it yet. need to work on it. Sorry for my bad english. Hope your solution will work

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.