按列值对多维数组的每个子数组进行排序

发布于 2024-11-25 10:09:27 字数 2462 浏览 5 评论 0原文

我有以下数组,我想按 value 对子数组进行排序。

[
    'bwin' => [
        ['bookie' => 'bwin', 'id_bookie' => 178537, 'value' => 6.00, 'bet' => 1],
        ['bookie' => 'bwin', 'id_bookie' => 178537, 'value' => 1.45, 'bet' => 2],
        ['bookie' => 'bwin', 'id_bookie' => 178537, 'value' => 4.50, 'bet' => 'x']
    ],
    'NordicBet' => [
        ['bookie' => 'NordicBet', 'id_bookie' => 201581, 'value' => 5.75, 'bet' => 1],
        ['bookie' => 'NordicBet', 'id_bookie' => 201581, 'value' => 1.50, 'bet' => 2],
        ['bookie' => 'NordicBet', 'id_bookie' => 201581, 'value' => 4.30, 'bet' => 'x']
    ],
    'Canbet' => [
        ['bookie' => 'Canbet', 'id_bookie' => 176582, 'value' => 5.60, 'bet' => 1],
        ['bookie' => 'Canbet', 'id_bookie' => 176582, 'value' => 1.56, 'bet' => 2],
        ['bookie' => 'Canbet', 'id_bookie' => 176582, 'value' => 3.80, 'bet' => 'x']
    ],
    'Expekt' => [
        ['bookie' => 'Expekt', 'id_bookie' => 235615, 'value' => 5.60, 'bet' => 1],
        ['bookie' => 'Expekt', 'id_bookie' => 235615, 'value' => 1.50, 'bet' => 2],
        ['bookie' => 'Expekt', 'id_bookie' => 235615, 'value' => 4.25, 'bet' => 'x']
    ],
    'StanJames' => [
        ['bookie' => 'StanJames', 'id_bookie' => 243649, 'value' => 5.50, 'bet' => 1],
        ['bookie' => 'StanJames', 'id_bookie' => 243649, 'value' => 1.53, 'bet' => 2],
        ['bookie' => 'StanJames', 'id_bookie' => 243649, 'value' => 4.00, 'bet' => 'x']
    ],
    'Gamebookers' => [
        ['bookie' => 'Gamebookers', 'id_bookie' => 203620, 'value' => 5.00, 'bet' => 1],
        ['bookie' => 'Gamebookers', 'id_bookie' => 203620, 'value' => 1.50, 'bet' => 2],
        ['bookie' => 'Gamebookers', 'id_bookie' => 203620, 'value' => 4.25, 'bet' => 'x']
    ],
    'Tipp3' => [
        ['bookie' => 'Tipp3', 'id_bookie' => 292604, 'value' => 4.10, 'bet' => 1],
        ['bookie' => 'Tipp3', 'id_bookie' => 292604, 'value' => 1.50, 'bet' => 2],
        ['bookie' => 'Tipp3', 'id_bookie' => 292604, 'value' => 3.40, 'bet' => 'x']
    ]
];

这是未排序的数组输出,但我想对行为 2 的数组进行排序,但行会发生变化,有时我必须对行为 x 或 1 的位置进行排序。

在此处输入图像描述

I have the following array and I want to sort the subarrays by value.

[
    'bwin' => [
        ['bookie' => 'bwin', 'id_bookie' => 178537, 'value' => 6.00, 'bet' => 1],
        ['bookie' => 'bwin', 'id_bookie' => 178537, 'value' => 1.45, 'bet' => 2],
        ['bookie' => 'bwin', 'id_bookie' => 178537, 'value' => 4.50, 'bet' => 'x']
    ],
    'NordicBet' => [
        ['bookie' => 'NordicBet', 'id_bookie' => 201581, 'value' => 5.75, 'bet' => 1],
        ['bookie' => 'NordicBet', 'id_bookie' => 201581, 'value' => 1.50, 'bet' => 2],
        ['bookie' => 'NordicBet', 'id_bookie' => 201581, 'value' => 4.30, 'bet' => 'x']
    ],
    'Canbet' => [
        ['bookie' => 'Canbet', 'id_bookie' => 176582, 'value' => 5.60, 'bet' => 1],
        ['bookie' => 'Canbet', 'id_bookie' => 176582, 'value' => 1.56, 'bet' => 2],
        ['bookie' => 'Canbet', 'id_bookie' => 176582, 'value' => 3.80, 'bet' => 'x']
    ],
    'Expekt' => [
        ['bookie' => 'Expekt', 'id_bookie' => 235615, 'value' => 5.60, 'bet' => 1],
        ['bookie' => 'Expekt', 'id_bookie' => 235615, 'value' => 1.50, 'bet' => 2],
        ['bookie' => 'Expekt', 'id_bookie' => 235615, 'value' => 4.25, 'bet' => 'x']
    ],
    'StanJames' => [
        ['bookie' => 'StanJames', 'id_bookie' => 243649, 'value' => 5.50, 'bet' => 1],
        ['bookie' => 'StanJames', 'id_bookie' => 243649, 'value' => 1.53, 'bet' => 2],
        ['bookie' => 'StanJames', 'id_bookie' => 243649, 'value' => 4.00, 'bet' => 'x']
    ],
    'Gamebookers' => [
        ['bookie' => 'Gamebookers', 'id_bookie' => 203620, 'value' => 5.00, 'bet' => 1],
        ['bookie' => 'Gamebookers', 'id_bookie' => 203620, 'value' => 1.50, 'bet' => 2],
        ['bookie' => 'Gamebookers', 'id_bookie' => 203620, 'value' => 4.25, 'bet' => 'x']
    ],
    'Tipp3' => [
        ['bookie' => 'Tipp3', 'id_bookie' => 292604, 'value' => 4.10, 'bet' => 1],
        ['bookie' => 'Tipp3', 'id_bookie' => 292604, 'value' => 1.50, 'bet' => 2],
        ['bookie' => 'Tipp3', 'id_bookie' => 292604, 'value' => 3.40, 'bet' => 'x']
    ]
];

This is the unsorted array output, but I want to sort the array where the line is 2, but the line will be changing, sometimes I have to sort where the line is x or 1.

enter image description here

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

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

发布评论

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

评论(2

耶耶耶 2024-12-02 10:09:27

迭代集合并通过引用修改行。
调用 usort()value 列值对子集进行排序。

代码:(演示)

foreach ($array as &$row) {
    usort($row, fn($a, $b) => $b['value'] <=> $a['value']);
}
var_export($array);

Iterate over the sets and modify the rows by reference.
Call usort() to sort the subsets by the value column values.

Code: (Demo)

foreach ($array as &$row) {
    usort($row, fn($a, $b) => $b['value'] <=> $a['value']);
}
var_export($array);
隔岸观火 2024-12-02 10:09:27

您可以使用 usort。这是一个小片段。在此功能中您几乎可以做您想做的事情。

function sorting_function($x, $y)
{
    if ($x['value'] == $y['value'])
        return 0;
    else if ($x['value'] < $y['value'])
        return -1;
    else
        return 1;
}

usort($table_array, 'sorting_function');

You can use usort. Here's a small snippet. You can do pretty much want you want in this function.

function sorting_function($x, $y)
{
    if ($x['value'] == $y['value'])
        return 0;
    else if ($x['value'] < $y['value'])
        return -1;
    else
        return 1;
}

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