2

My multidimensional array is sorted alphabetically by the OwnerNickName field, but I need to make the OwnerNickName = 'My House' row as the sticky first row and then everything else sorted by OwnerNickname ascending.

Input:

[
    '0318B69D-5DEB-11DF-9D7E-0026B9481364' => [
        'OwnerNickName' => 'andy',
        'Rooms' => [ /* irrelevant */ ]
    ],
    '286C29DE-A9BE-102D-9C16-00163EEDFCFC' => [
        'OwnerNickName' => 'anton',
        'Rooms' => [ /* irrelevant */ ]
    ],
    '8BE18F84-AC22-102D-9C16-00163EEDFCFC' => [
        'OwnerNickName' => 'mike',
        'Rooms' => [ /* irrelevant */ ]
    ],
    '29B455DE-A9BC-102D-9C16-00163EEDFCFC' => [
        'OwnerNickName' => 'My House',
        'Rooms' => [ /* irrelevant */ ]
    ]
]
2

3 Answers 3

5

You may want to implement your own sorting function, e.g.:

function cmp($a, $b)
{
    if ($a['OwnerNickName'] == $b['OwnerNickName']) {
        return 0;
    }
    if ($a['OwnerNickName'] == 'My House') {
        return -1;
    } else if ($b['OwnerNickName'] == 'My House') {
        return 1;
    }
    return ($a['OwnerNickName'] < $b['OwnerNickName']) ? -1 : 1;
}    
usort($array, 'cmp');
Sign up to request clarification or add additional context in comments.

1 Comment

Maybe use uasort if you want to keep your keys
1

If you want to change your mind about which index to sort on or which value should be special, something like this might suit:

function specialSort(array &$array, $index, $specialvalue){
    $callback = function($a,$b) use ($index, $specialvalue) {  //closure
       if ($a[$index] == $b[$index]) return 0;
       if ($a[$index] == $specialvalue) return -1;
       if ($b[$index] == $specialvalue) return 1;
       return ($a[$index] < $b[$index]) ? -1 : 1;
    }  ;

    uasort($array, $callback);
}

$arr=array(
    'a'=>array('thing'=>'yay','who'=>'owee'),
    'foo'=>array('thing'=>'boo','who'=>'wik'),
    'd'=>array('thing'=>'kil','who'=>'ilo'),
    'b'=>array('thing'=>'ser','who'=>'uyt'),
    'zed'=>array('thing'=>'efv','who'=>'qet')
);

specialSort($arr,'who','ilo');
print_r($arr);

Gives the result:

Array
(
    [d] => Array
        (
            [thing] => kil
            [who] => ilo //special value
        )

    [a] => Array
        (
            [thing] => yay
            [who] => owee
        )

    [zed] => Array
        (
            [thing] => efv
            [who] => qet
        )

    [b] => Array
        (
            [thing] => ser
            [who] => uyt
        )

    [foo] => Array
        (
            [thing] => boo
            [who] => wik
        )

)

Comments

0

To preserve the original first level keys, uasort(). Pass the sorting rules to the callback as two-element arrays to be compared by the spaceship operator. Because false is "less than" true, check if the OwnerNickName is NOT equal to My House to make it the sticky first element.

Code: (Demo)

uasort(
    $array,
    fn($a, $b) => [$a['OwnerNickName'] !== 'My House', $a['OwnerNickName']]
                  <=>
                  [$b['OwnerNickName'] !== 'My House', $b['OwnerNickName']]
);
var_export($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.