按每行中第一个子数组的文本对数组进行排序

Sorting an array by text of first subarray in each row

我有一个存储为 $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] 中的键 'text' 对其进行排序,这会给我一个结果

[
    [
        ['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() 只需 returns 1.

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

你的数组:

$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',
        ],
    ],
]

您只需要使用您用英语表达的数组语法访问子数组数据。 (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']);