-1

我有这个数组:

[0] => Array
    (


        [tags] => SimpleXMLElement Object
            (
                [0] => MusicVideos, #20
            )

    )

[1] => Array
    (

        [tags] => SimpleXMLElement Object
            (
                [0] => MusicVideos, #5
            )

    )

[2] => Array
    (


        [tags] => SimpleXMLElement Object
            (
                [0] => #9, MusicVideos
            )

    )

[3] => Array
    (

        [tags] => SimpleXMLElement Object
            (
                [0] => #12, MusicVideos
            )

    )

现在我想通过 [tags] 对这个数组进行排序,我该如何做这个 PHP?使用在线工具 [标签] 在我的网站上按我想要的“#1”、“#2”订购

但我比 [Tags] 多一个,例如“#1”和“Category”

[Tags] => #1 [Tags] => MusicVideos, #2 [Tags] => #3

可以这样订购吗?

我尝试使用此功能:

function msort($array, $key, $sort_flags = SORT_NUMERIC) {
if (is_array($array) && count($array) > 0) {
    if (!empty($key)) {
        $mapping = array();
        foreach ($array as $k => $v) {
            $sort_key = '';
            if (!is_array($key)) {
                $sort_key = $v[$key];
            } else {
                // @TODO This should be fixed, now it will be sorted as string
                foreach ($key as $key_key) {
                    $sort_key .= $v[$key_key];
                }
                $sort_flags = SORT_STRING;
            }
            $mapping[$k] = $sort_key;
        }
        asort($mapping, $sort_flags);
        $sorted = array();
        foreach ($mapping as $k => $v) {
            $sorted[] = $array[$k];
        }
        return $sorted;
    }
}
return $array;

}

但是这个 SORT by ALL 在 NAME 和下一个 NUMBER 之后引入了 EMPTY

[0] => Array
    (
        [tags] => SimpleXMLElement Object
            (
                [0] => #5
            )

    )

[1] => Array
    (
        [tags] => SimpleXMLElement Object
            (
                [0] => Commercials, #3
            )

    )

[2] => Array
    (
        [tags] => SimpleXMLElement Object
            (
                [0] => Commercials, #9
            )


    )

[3] => Array
    (
        [tags] => SimpleXMLElement Object
            (
                [0] => MusicVideos, #1
            )

    )

[4] => Array
    (
        [tags] => SimpleXMLElement Object
            (
                [0] => MusicVideos, #10
            )


    )

我想要这样的东西:

[0] => Array
    (
        [tags] => SimpleXMLElement Object
            (
                [0] => MusicVideos, #1
            )

    )

[1] => Array
    (
        [tags] => SimpleXMLElement Object
            (
                [0] => Commercials, #3
            )

    )

[2] => Array
    (
        [tags] => SimpleXMLElement Object
            (
                [0] => #5
            )

    )

[3] => Array
    (
        [tags] => SimpleXMLElement Object
            (
                [0] => Commercials, #9
            )

    )

[4] => Array
    (
        [tags] => SimpleXMLElement Object
            (
                [0] => MusicVideos, #10
            )

    )

谢谢

4

1 回答 1

0

您应该能够将usort与适当的可调用对象一起使用来进行排序。

排序函数可以使用任何逻辑来提取排序字段。如果要按以#数字开头的标签进行排序,则需要在标签字段中找到该子字符串(此处使用正则表达式):

usort($arr, function($a, $b) {
    $matches = array();
    preg_match('/#(\d+)/', (string) $a['tags'], $matches);
    $aTagNo = (int) $matches[1];
    preg_match('/#(\d+)/', (string) $b['tags'], $matches);
    $bTagNo = (int) $matches[1];

    if ($aTagNo < $bTagNo)
        return -1;
    else if ($aTagNo > $bTagNo)
        return 1;
    else
        return 0;
});
于 2013-10-02T03:16:15.447 回答