Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Need sort multidimensional associative array with numeric and string keys. I have an array in this format:

$arr = array(
    0 => 'bbb',
    'bbb' => array(),
    'ccc' => array(
        0 => 'bbb',
        'bbb' => array(),
        'ccc' => array(),
        '&^%' => array(),
        1 => 'ccc',
        'aaa' => array(),
        2 => 'aaa',
    ),
    '&^%' => array(),
    1 => 'ccc',
    'aaa' => array(),
    2 => 'aaa',
);

final results needs to be:

$arr_result = array(
    '&^%' => array(),
    'aaa' => array(),
    'bbb' => array(),
    'ccc' => array(
        '&^%' => array(),
        'aaa' => array(),
        'bbb' => array(),
        'ccc' => array(),
        2 => 'aaa',
        0 => 'bbb',
        1 => 'ccc',
    ),
    2 => 'aaa',
    0 => 'bbb',
    1 => 'ccc',
);

need to elements with string keys were sorted by the keys in alphabetically order, and elements with numeric keys in alphabetically order of their values. Numeric keys don't necessarily maintain. Quantity of dimensions can be different, generated dynamically. Thanks.

share|improve this question

closed as unclear what you're asking by kingkero, Amal Murali, Tomasz Kowalczyk, nKn, David Apr 26 at 12:36

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

    
What did you try already? –  kingkero Apr 26 at 10:09
1  
you need to sort only alphabet? or else what you have tried? –  VIVEK-MDU Apr 26 at 10:11
    
only alphabet. I'm tried write function with uksort, but I have bad result –  merovinh Apr 26 at 10:32

1 Answer 1

Use this function for sort:

function special_sort(array &$array) {
    static $cmp;
    if ($cmp === null) {
        $cmp = function ($key_a, $key_b) use ($array) {
            $value_a = $array[$key_a];
            $value_b = $array[$key_b];
            $is_array_a = is_array($value_a);
            $is_array_b = is_array($value_b);
            if ($is_array_a && $is_array_b) {
                if ($key_a == $key_b) {
                    return 0;
                }
                return $key_a < $key_b ? -1 : 1;
            }
            if ($is_array_a) {
                return -1;
            }
            if ($is_array_b) {
                return 1;
            }
            if ($value_a == $value_b) {
                return 0;
            }
            return $value_a < $value_b ? -1 : 1;
        };
    }
    foreach ($array as &$value) {
        if (is_array($value)) {
            special_sort($value);
        }
    }
    uksort($array, $cmp); 
}
share|improve this answer
    
Great work! Thank you very much! –  merovinh Apr 26 at 10:54
    
@merovinh Please choose my answer as best answer –  CertaiN Apr 26 at 10:55
    
I can't do it, because have a small reputation ( –  merovinh Apr 26 at 10:59
    
@merovinh oh, I see xD –  CertaiN Apr 26 at 11:41

Not the answer you're looking for? Browse other questions tagged or ask your own question.