-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 を実行できますか? オンライン ツール [タグ] を使用して、私のウェブサイトで i want "#1", "#2" で注文します。

しかし、私は [タグ] のように「#1」と「カテゴリ」よりも 1 つ多く持っています

[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

ソートを行うために適切な callable で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 に答える