如何使用array_multisort()对数组进行排序?

时间:2019-05-31 10:04:35

标签: php arrays sorting

我有以下PHP代码

<?php
$colors = array(
            array (
        "checked" => '',
        "intype" => '',
        "id" => '',
        "store1" => 'Red',
        "defaultswatch1" => '',
        "swatch1" => '',
        "store0" => 'Red',
        "defaultswatch0" => '',
        "swatch0" => '',
            ),
             array (
        "checked" => '',
        "intype" => '',
        "id" => '',
        "store1" => 'Black',
        "defaultswatch1" => '',
        "swatch1" => '',
        "store0" => 'Black',
        "defaultswatch0" => '/t/h/th-petal_pink_9_1.jpg',
        "swatch0" => 'url(https://staging.zitomer.com/media/attribute/swatch/t/h/th-petal_pink_9_1.jpg); background-size: cover;',
            ),
             array (
        "checked" => '',
        "intype" => '',
        "id" => '',
        "store1" => 'Green',
        "defaultswatch1" => '',
        "swatch1" => '',
        "store0" => 'Green',
        "defaultswatch0" =>'',
        "swatch0" =>'',
            ),
         );

foreach($colors as $key => $val) {
$mid[ucwords($key)]  = ucwords($val['store0']);
}
array_multisort($mid, SORT_ASC, $colors);
print_r($colors);
?>

当我尝试使用array_multisort()根据'store0'键对数组进行排序时,它可以正确排序。参见下面的输出。

实际输出越来越差,排序是bvelow。 但是在这里,键“ store0”的黑色值未正确排序。它分配给最后一个数组。

Array
(
    [0] => Array
        (
            [checked] =>
            [intype] =>
            [store1] => Black
            [defaultswatch1] =>
            [swatch1] =>
            [store0] => Black
            [defaultswatch0] =>
            [swatch0] =>
        )

    [1] => Array
        (
            [checked] =>
            [intype] =>
            [store1] => Green
            [defaultswatch1] =>
            [swatch1] =>
            [store0] => Green
            [defaultswatch0] =>
            [swatch0] =>
        )

    [2] => Array
        (
            [checked] =>
            [intype] =>
            [store1] => Red
            [defaultswatch1] =>
            [swatch1] =>
            [store0] => Red
            [defaultswatch0] => /t/h/th-petal_pink_9_1.jpg
            [swatch0] => url(https://staging.zitomer.com/media/attribute/swatch/t/h/th-petal_pink_9_1.jpg); background-size: cover;
        )

)

预期的输出应该是排序的。

Array
(
    [0] => Array
        (
            [checked] =>
            [intype] =>
            [store1] => Black
            [defaultswatch1] =>
            [swatch1] =>
            [store0] => Black
            [defaultswatch0] => /t/h/th-petal_pink_9_1.jpg
            [swatch0] => url(https://staging.zitomer.com/media/attribute/swatch/t/h/th-petal_pink_9_1.jpg); background-size: cover;
        )

    [1] => Array
        (
            [checked] =>
            [intype] =>
            [store1] => Green
            [defaultswatch1] =>
            [swatch1] =>
            [store0] => Green
            [defaultswatch0] =>
            [swatch0] =>
        )

    [2] => Array
        (
            [checked] =>
            [intype] =>
            [store1] => Red
            [defaultswatch1] =>
            [swatch1] =>
            [store0] => Red
            [defaultswatch0] =>
            [swatch0] =>
        )

)

0 个答案:

没有答案
相关问题