【问题标题】:Sorting an array by text of first subarray in each row按每行中第一个子数组的文本对数组进行排序
【发布时间】:2021-11-16 23:59:58
【问题描述】:

我有一个存储为 $product_categories 的数组。该数组的一个示例是:

$array = [
    [
        ['id' => 10, 'text' => 'Latex'],
        ['id' => 15, 'text' => 'Occasion Latex'],
        ['id' => 82, 'text' => 'Christmas'],
    ],
    [
        ['id' => 11, 'text' => 'Accessories'],
        ['id' => 97, 'text' => 'Retail Accessories'],
        ['id' => 558, 'text' => 'Super Stuffer'],
    ],
    [
        ['id' => 374, 'text' => 'Party Supplies'],
        ['id' => 1488, 'text' => 'Party by Occasion'],
        ['id' => 1493, 'text' => 'Christmas'],
    ],
];

我只想按 [0] 中的“文本”键对其进行排序,这会给我一个结果

[
    [
        ['id' => 11, 'text' => 'Accessories'],
        ['id' => 97, 'text' => 'Retail Accessories'],
        ['id' => 558, 'text' => 'Super Stuffer'],
    ],
    [
        ['id' => 10, 'text' => 'Latex'],
        ['id' => 15, 'text' => 'Occasion Latex'],
        ['id' => 82, 'text' => 'Christmas'],
    ],
    [
        ['id' => 374, 'text' => 'Party Supplies'],
        ['id' => 1488, 'text' => 'Party by Occasion'],
        ['id' => 1493, 'text' => 'Christmas'],
    ],
];

我试过了

$product_categories = usort($product_categories, 'sortAlphabetically');

function sortAlphabetically($a, $b) {
    return strcmp($a['text'], $b['text']);
}

使用它,数组的 print_r() 简单地返回 1.

我认为usort() 是对数组进行排序的正确方法,但显然我在这里做错了。

【问题讨论】:

  • usort() 对数组进行就地排序并返回TRUE,它不返回排序后的数组。
  • $a['text'] 应该是$a[0]['text'],如果你想按[0] 中的文本排序,$b 也是如此。

标签: php arrays usort


【解决方案1】:

您只需要使用您用英语表达的数组语法访问子数组数据。 (Demo)

usort(
    $array,
    function($a, $b) {
        return $a[0]['text'] <=> $b[0]['text'];
    }
);
var_export($array);

或 PHP7.4 或更高版本:

usort($array, fn($a, $b) => $a[0]['text'] <=> $b[0]['text']);

【讨论】:

    【解决方案2】:

    你的数组:

    $array = [
        '0' => [
            '0' => [
                'id'   => 10,
                'text' => 'Latex',
            ],
            '1' => [
                'id'   => 15,
                'text' => 'Occasion Latex',
            ],
            '2' => [
                'id'   => 82,
                'text' => 'Christmas',
            ],
        ],
        '1' => [
            '0' => [
                'id'   => 11,
                'text' => 'Accessories',
            ],
            '1' => [
                'id'   => 97,
                'text' => 'Retail Accessories',
            ],
            '2' => [
                'id'   => 558,
                'text' => 'Super Stuffer',
            ],
        ],
        '2' => [
            '0' => [
                'id'   => 374,
                'text' => 'Party Supplies',
            ],
            '1' => [
                'id'   => 1488,
                'text' => 'Party by Occasion',
            ],
            '2' => [
                'id'   => 1493,
                'text' => 'Christmas',
            ],
        ],
    ];
    

    排序:

    // uasort(): "Sort an array with a user-defined comparison function
    //            and maintain index association"
    uasort($array, function (array $a, array $b) {
        // "I want to sort it ONLY by the key 'text' in [0]"
        // Get first from array (aka [0]).
        // (You could change that to fix $a[0] and $b[0] if you want|need to.)
        $aFirst = reset($a); 
        $bFirst = reset($b); 
        $offset = 'text';
        if ($aFirst[$offset] == $bFirst[$offset]) {
            return 0;
        }
        // a < b === asc ; a > b === desc
        return ($aFirst[$offset] < $bFirst[$offset]) ? -1 : 1;
    });
    echo var_export($array, true) . PHP_EOL;
    

    结果:

    [
        1 => [
            0 => [
                'id'   => 11,
                'text' => 'Accessories',
            ],
            1 => [
                'id'   => 97,
                'text' => 'Retail Accessories',
            ],
            2 => [
                'id'   => 558,
                'text' => 'Super Stuffer',
            ],
        ],
        0 => [
            0 => [
                'id'   => 10,
                'text' => 'Latex',
            ],
            1 => [
                'id'   => 15,
                'text' => 'Occasion Latex',
            ],
            2 => [
                'id'   => 82,
                'text' => 'Christmas',
            ],
        ],
        2 => [
            0 => [
                'id'   => 374,
                'text' => 'Party Supplies',
            ],
            1 => [
                'id'   => 1488,
                'text' => 'Party by Occasion',
            ],
            2 => [
                'id'   => 1493,
                'text' => 'Christmas',
            ],
        ],
    ]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-17
      • 1970-01-01
      • 1970-01-01
      • 2018-07-16
      • 1970-01-01
      • 1970-01-01
      • 2019-06-30
      • 1970-01-01
      相关资源
      最近更新 更多