当找到特定值时,按列值对多维数组的行进行排序,并使用粘性第一行

发布于 2024-09-03 20:32:19 字数 736 浏览 1 评论 0原文

我的多维数组按 OwnerNickName 字段按字母顺序排序,但我需要将 OwnerNickName = 'My House' 行作为粘性第一行,然后按 OwnerNickname 排序其他所有内容上升。

输入:

[
    '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 */ ]
    ]
]

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 */ ]
    ]
]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浅浅淡淡 2024-09-10 20:32:19

您可能想实现自己的排序功能,例如:

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');

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');
南风起 2024-09-10 20:32:19

如果您想改变对哪个索引进行排序或哪个值应该特殊的想法,类似这样的内容可能适合:

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);

给出结果:

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
        )

)

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
        )

)
╭ゆ眷念 2024-09-10 20:32:19

要保留原始的第一级键,uasort()。将排序规则作为二元素数组传递给回调函数,以便由 spaceship 运算符进行比较。由于 false“小于”true,因此请检查 OwnerNickName 是否不等于 My House,以使其成为粘性第一个元素。

代码:(演示)

uasort(
    $array,
    fn($a, $b) => [$a['OwnerNickName'] !== 'My House', $a['OwnerNickName']]
                  <=>
                  [$b['OwnerNickName'] !== 'My House', $b['OwnerNickName']]
);
var_export($array);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文