简体   繁体   中英

Sorting multi-dimensional associative array in PHP

I have a PHP array that looks like this:

array (size=1)
  'Marriot' => 
    array (size=7)
      0 => string 'doc5.txt' (length=8)
      1 => string 'test.txt' (length=8)
      2 => string 'test1.txt' (length=9)
      3 => string 'test2.txt' (length=9)
      4 => string 'test3.txt' (length=9)
      5 => 
        array (size=1)
          'Special Docs' => 
            array (size=2)
              0 => string 'doc4.txt' (length=8)
              1 => string 'doc3.txt' (length=8)
      6 => 
        array (size=1)
          'ADocs' => 
            array (size=0)
              empty

As you can see, it holds non-associative files, and then two folders, "Special Docs" and "ADocs". My problem is two-fold:

First, I want to move the two folders to the top of the array so that they will be prominent in my view. Second, I want to sort the folders alphabetically (ie put "ADocs" above "Special Docs". I have tried array_multisort without success and am sort of stuck here. Anyone know how I might achieve this?

Thanks for your help.

Example Input

$dir = array(
    0 => 'doc5.txt',
    1 => 'test.txt',
    2 => 'test1.txt',
    3 => 'test2.txt',
    4 => 'test3.txt',
    5 => array(
        'Special Docs' => array (
              0 => 'doc4.txt',
              1 => 'doc3.txt'
          )
    ),
    6 => array( 
        'ADocs' => array()
    )
);

1 level sorting

function cmp ($a,$b) {
    if (is_array($a)){
        if (is_array ($b)) {
            return strnatcasecmp (key($a), key($b));
        } else {
            return -1;
        }
    } else {
        if (is_array ($b)) {
            return 1;
        } else {
            return strnatcasecmp ($a, $b);
        }
    }
}

Example Output

Array
(
    [0] => Array
        (
            [ADocs] => Array
                (
                )

        )

    [1] => Array
        (
            [Special Docs] => Array
                (
                    [0] => doc4.txt
                    [1] => doc3.txt
                )

        )

    [2] => doc5.txt
    [3] => test.txt
    [4] => test1.txt
    [5] => test2.txt
    [6] => test3.txt
)

Multi level sorting (unlimited)

function cmp (&$a,&$b) {
    if (is_array($a)){
        usort($a[key($a)], 'cmp');
        if (is_array ($b)) {
            return strnatcasecmp (key($a), key($b));
        } else {
            return -1;
        }
    } else {
        if (is_array ($b)) {
            return 1;
        } else {
            return strnatcasecmp ($a, $b);
        }
    }
}

usort ($dir, 'cmp');

Example Output

Array
(
    [0] => Array
        (
            [ADocs] => Array
                (
                )

        )

    [1] => Array
        (
            [Special Docs] => Array
                (
                    [0] => doc3.txt
                    [1] => doc4.txt
                )

        )

    [2] => doc5.txt
    [3] => test.txt
    [4] => test1.txt
    [5] => test2.txt
    [6] => test3.txt
)

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