按第一级键对 3d 数组进行排序,然后按单独的第三级键对第二级进行排序

发布于 2024-09-04 22:28:29 字数 792 浏览 15 评论 0原文

我有一个像这样的多维数组:

[
    'user 1' => [
        ['item 4' => '11385'],
        ['item 2' => '144268'],
        ['item 1' => '65774'],
        ['item 9' => '98523'],
    ],
    'user 5' => [
        ['item 8' => '239233'],
    ],
    'user 2' => [
        ['item 4' => '53718'],
        ['item 1' => '154687'],
    ],
]

我需要做的就是首先按用户(第一级键)对数组进行排序,然后按项目(第三级键)升序排序。

期望的结果:

[
    'user 1' => [
        ['item 1' => '65774'],
        ['item 2' => '144268'],
        ['item 4' => '11385'],
        ['item 9' => '98523'],
    ],
    'user 2' => [
        ['item 1' => '154687'],
        ['item 4' => '53718'],
    ],
    'user 5' => [
        ['item 8' => '239233'],
    ],
]

I have a multidimensional array like this:

[
    'user 1' => [
        ['item 4' => '11385'],
        ['item 2' => '144268'],
        ['item 1' => '65774'],
        ['item 9' => '98523'],
    ],
    'user 5' => [
        ['item 8' => '239233'],
    ],
    'user 2' => [
        ['item 4' => '53718'],
        ['item 1' => '154687'],
    ],
]

What I need to do is to simply sort my array first by users (first level keys), then by items (third level keys) ascending.

Desired result:

[
    'user 1' => [
        ['item 1' => '65774'],
        ['item 2' => '144268'],
        ['item 4' => '11385'],
        ['item 9' => '98523'],
    ],
    'user 2' => [
        ['item 1' => '154687'],
        ['item 4' => '53718'],
    ],
    'user 5' => [
        ['item 8' => '239233'],
    ],
]

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

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

发布评论

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

评论(3

○愚か者の日 2024-09-11 22:28:29

ksort 按键

$newArray = array();

# start by sorting users
$yourArray = ksort($yourArray);

# then sort sub items
foreach($yourArray as $user=>$theirItems) {
    $theirItems = ksort($theirItems); # assuming you still want to sort by key;
    $newArray[$user] = $theirItems;
}

var_dump($newArray);

排序输出:(演示)

Warning: foreach() argument must be of type array|object, true given

ksort sorts by the key

$newArray = array();

# start by sorting users
$yourArray = ksort($yourArray);

# then sort sub items
foreach($yourArray as $user=>$theirItems) {
    $theirItems = ksort($theirItems); # assuming you still want to sort by key;
    $newArray[$user] = $theirItems;
}

var_dump($newArray);

Output: (Demo)

Warning: foreach() argument must be of type array|object, true given
兮子 2024-09-11 22:28:29

使用 usort() 然后创建两个比较函数并排序你的数组一一使用它们。

Use usort() then create two comparison functions and sort your array using them one after one.

家住魔仙堡 2024-09-11 22:28:29

要按第一级键排序,然后按每个子数组中 firat 元素的键排序,请调用 ksort(),然后按单个元素的键在每组数据中排序。

代码:(演示)

ksort($array);
foreach ($array as &$v) {
    usort(
        $v,
        fn($a, $b) => key($a) <=> key($b)
    );
}
var_export($array);

或者使用 array_myltisort() 来减少总迭代函数调用: (演示

ksort($array);
foreach ($array as &$v) {
    array_multisort(
        array_map('key', $v),
        $v
    );
}
var_export($array);

To sort by first level keys, then by the key of the firat element in each subarray, call ksort() then sort within each set of data by the lone element's key.

Code: (Demo)

ksort($array);
foreach ($array as &$v) {
    usort(
        $v,
        fn($a, $b) => key($a) <=> key($b)
    );
}
var_export($array);

Or with array_myltisort() for fewer total iterated function calls: (Demo)

ksort($array);
foreach ($array as &$v) {
    array_multisort(
        array_map('key', $v),
        $v
    );
}
var_export($array);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文