如何在此代码中使用 `_key` 对数组进行排序? [复制]

Posted

技术标签:

【中文标题】如何在此代码中使用 `_key` 对数组进行排序? [复制]【英文标题】:How to sort the array by using `_key` in this code? [duplicate] 【发布时间】:2016-12-31 05:22:41 【问题描述】:

如何在这段代码中使用_key对数组进行排序?

Array
    (
        [0] => stdClass Object
            (
                [_key] => 50180
                [group] => stdClass Object
                    (
                        [id] => 50180
                        [name] => CriticalChain
                    )

            )

        [1] => stdClass Object
            (
                [_key] => 2357895
                [group] => stdClass Object
                    (
                        [id] => 2357895
                        [name] => Data Modeling
                    )

            )

        [2] => stdClass Object
            (
                [_key] => 1992105
                [group] => stdClass Object
                    (
                        [id] => 1992105
                        [name] => SQL Server Users in Israel
                    )

            )

        [3] => stdClass Object
            (
                [_key] => 37988
                [group] => stdClass Object
                    (
                        [id] => 37988
                        [name] => CDO/CIO/CTO Leadership Council
                    )

            )

        [4] => stdClass Object
            (
                [_key] => 4024801
                [group] => stdClass Object
                    (
                        [id] => 4024801
                        [name] => BiT-HR, BI & IT Placement Agency
                    )

            )

        [5] => stdClass Object
            (
                [_key] => 37845
                [group] => stdClass Object
                    (
                        [id] => 37845
                        [name] => Israel Technology Group
                    )

            )

        [6] => stdClass Object
            (
                [_key] => 51464
                [group] => stdClass Object
                    (
                        [id] => 51464
                        [name] => Israel DBAs
                    )

            )

        [7] => stdClass Object
            (
                [_key] => 66097
                [group] => stdClass Object
                    (
                        [id] => 66097
                        [name] => SQLDBA
                    )

            )

        [8] => stdClass Object
            (
                [_key] => 4462353
                [group] => stdClass Object
                    (
                        [id] => 4462353
                        [name] => Israel High-Tech Group
                    )

            )

        [9] => stdClass Object
            (
                [_key] => 4203807
                [group] => stdClass Object
                    (
                        [id] => 4203807
                        [name] => Microsoft Team Foundation Server
                    )

            )

    )

【问题讨论】:

使用ksort 【参考方案1】:

使用usort 或循环使用 _key 作为数组的键并按键对数组进行排序:

$newArray = array();
foreach($arr as $item)
    $newArray[$item->key] = $item;

ksort($newArray);

【讨论】:

以上是关于如何在此代码中使用 `_key` 对数组进行排序? [复制]的主要内容,如果未能解决你的问题,请参考以下文章

按数组中的数组对对象进行排序?

如何一次对字典的所有键、子键、子子键等进行排序?

对数组中的字典进行排序

如何通过 valueForKey 从 fetch 中对数组进行排序

如何使用节点 js 对 mongoDB 中的集合内的数组进行排序

redis 如何存一个PHP数组 代码怎么写呢