Frage

Ich habe einen PHP-Array, das ich sortieren muß. Ich habe das Beispiel Array unten enthalten. Ich brauche die Top-10 Anzahl der URLs und ihre Perspektive Zählungen in einer anderen Anordnung zu platzieren. Ich weiß, ich könnte in Problem führen, wenn es nicht mehr als 10 Top-Spiele ... wenn das passiert dann eine zufällige Übereinstimmungen in Ordnung sein würde.

Irgendwelche Vorschläge?

Ich habe sort (myarray) versucht, aber das sortiert nur das erste Objekt in dem Array ich es brauche die zweite zu sortieren.

Irgendwelche Ideen?


Array
(
    [0] => Array
        (
            [name] => http://bit.ly/2oUTzf
            [count] => 1
        )

    [1] => Array
        (
            [name] => http://tiny.cc/wyNbi
            [count] => 1
        )

    [2] => Array
        (
            [name] => http://ow.ly/Almo
            [count] => 1
        )

    [3] => Array
        (
            [name] => http://bit.ly/7bQ8sY
            [count] => 1
        )

    [4] => Array
        (
            [name] => http://kissa.be/w4V-
            [count] => 5
        )

    [5] => Array
        (
            [name] => http://ow.ly/xzwI
            [count] => 1
        )

    [6] => Array
        (
            [name] => http://twa.lk/L6FZX
            [count] => 1
        )

    [7] => Array
        (
            [name] => http://tinyurl.com/Alyssa10
            [count] => 1
        )

    [8] => Array
        (
            [name] => http://www.hiderefer.com/0cz7kNgA.htm
            [count] => 1
        )

    [9] => Array
        (
            [name] => http://tinyurl.com/Joanie515
            [count] => 1
        )

    [10] => Array
        (
            [name] => http://ow.ly/uJvB
            [count] => 1
        )

    [11] => Array
        (
            [name] => http://tinyurl.com/
            [count] => 1
        )

    [12] => Array
        (
            [name] => http://www.hiderefer.com/wJBUhh3G.htm
            [count] => 1
        )

    [13] => Array
        (
            [name] => http://short.to/xcxc
            [count] => 1
        )

    [14] => Array
        (
            [name] => http://bit.ly/t79FA
            [count] => 2
        )

    [15] => Array
        (
            [name] => http://tinyurl.com/yzy33yl
            [count] => 1
        )

    [16] => Array
        (
            [name] => http://p.gs/zksz6
            [count] => 1
        )

    [17] => Array
        (
            [name] => http://bit.ly/7E1cc8
            [count] => 1
        )

    [18] => Array
        (
            [name] => http://bit.ly/6hbugu
            [count] => 1
        )

    [19] => Array
        (
            [name] => http://tii.libsyn.com/index.php
            [count] => 6
        )

    [20] => Array
        (
            [name] => http://tinyurl.com/nlzzwq
            [count] => 1
        )

    [21] => Array
        (
            [name] => http://bit.ly/7gAdXi
            [count] => 1
        )

    [22] => Array
        (
            [name] => http://localtweeps.com
            [count] => 1
        )

    [23] => Array
        (
            [name] => http://localtweeps.com.
            [count] => 3
        )

    [24] => Array
        (
            [name] => http://scribd.com/doc/22365778
            [count] => 1
        )

    [25] => Array
        (
            [name] => http://quick-weight-loss-secrets.com/
            [count] => 1
        )

    [26] => Array
        (
            [name] => http://tinyurl.com/ykd5qm5
            [count] => 1
        )

    [27] => Array
        (
            [name] => http://bit.ly/5DQ6SO
            [count] => 1
        )

    [28] => Array
        (
            [name] => http://bit.ly/4z6Kww
            [count] => 1
        )

    [29] => Array
        (
            [name] => http://bit.ly/40sm9N
            [count] => 1
        )

    [30] => Array
        (
            [name] => http://bit.ly/8mh7DO
            [count] => 5
        )

    [31] => Array
        (
            [name] => http://tinyurl.com/krt5yf
            [count] => 1
        )

    [32] => Array
        (
            [name] => http://bit.ly/7GsthV
            [count] => 1
        )

    [33] => Array
        (
            [name] => http://bit.ly/1QJzvM
            [count] => 1
        )

    [34] => Array
        (
            [name] => http://yfrog.com/1durkj
            [count] => 1
        )

    [35] => Array
        (
            [name] => http://budurl.com/dxwc
            [count] => 9
        )

    [36] => Array
        (
            [name] => http://digg.com/d1qiCr
            [count] => 1
        )

    [37] => Array
        (
            [name] => http://bit.ly/eVSIo
            [count] => 1
        )

    [38] => Array
        (
            [name] => http://yfrog.com/37badgj
            [count] => 2
        )

    [39] => Array
        (
            [name] => http://tinyurl.com/qh8sos
            [count] => 1
        )

    [40] => Array
        (
            [name] => http://tinyurl.com/mz7l8d
            [count] => 3
        )

    [41] => Array
        (
            [name] => http://tinyurl.com/nratac
            [count] => 1
        )

    [42] => Array
        (
            [name] => http://tinyurl.com/yk587jx
            [count] => 1
        )

    [43] => Array
        (
            [name] => http://www.bethel.edu/alumni/homecoming/09/events/
            [count] => 1
        )

    [44] => Array
        (
            [name] => http://www.waytofit.net
            [count] => 1
        )

    [45] => Array
        (
            [name] => http://twitpic.com/rdcy8
            [count] => 1
        )

    [46] => Array
        (
            [name] => http://retwt.me/1C1Vd
            [count] => 14
        )

    [47] => Array
        (
            [name] => http://www.starbucks.com/card
            [count] => 1
        )

    [48] => Array
        (
            [name] => http://tinyurl.com/yhkbfqe
            [count] => 13
        )

    [49] => Array
        (
            [name] => http://bit.ly/playspy
            [count] => 1
        )

    [50] => Array
        (
            [name] => http://bit.ly/57rHLO
            [count] => 12
        )
War es hilfreich?

Lösung

Sie müssen eine benutzerdefinierte Sortierfunktion schreiben - wie folgt aus:

function MyCustomSort($a, $b)
{
    if ($a->count == $b->count) {
        return 0;
    }
    return ($a->count < $b->count) ? -1 : 1;
}

Dann übergeben Sie diese Funktion in eine Art - wie folgt aus:

usort($myArray, "MyCustomSort");

Sie können auch eine Funktion schreiben Sie sortieren nach Website helfen - wie folgt aus:

function MyCustomSort($a, $b)
{
    if ($a->name == $b->name) {
        return 0;
    }
    return ($a->name < $b->name) ? -1 : 1;
}

Andere Tipps

Sie verwenden dekorieren-Art-undecorate Muster.

<?php

$arr = Array
(
    [0] => Array
        (
            [name] => http://bit.ly/2oUTzf
            [count] => 1
        )

    [1] => Array
        (
            [name] => http://tiny.cc/wyNbi
            [count] => 1
        )

    [2] => Array
        (
            [name] => http://ow.ly/Almo
            [count] => 1
        )

    [3] => Array
        (
            [name] => http://bit.ly/7bQ8sY
            [count] => 1
        )
...
);

// actual sorting below
$arr= array_map(create_function('$a', 'return array($a["count"], $a);'), $arr); // transform into array of arrays consisted of sort key and item
sort($arr); // sort array of arrays
$arr = array_map('end', $arr); // take only last element from each array

print_r($arr);

Wie funktioniert es?

Statt Array Ihrer Artikel zu sortieren Sie Array von Arrays, deren letztes Element sortieren Element ist und erste ist der Schlüssel, mit dem Sie sortieren möchten. Nach dem Sortieren halten Sie nur das Einzelteil.

Sie können nur für das Sortieren Array von Arrays verwenden sort weil PHP zwei Arrays dieselbe Länge vergleicht durch seine Elemente eines nach dem anderen verglichen wird.

Sortieren nach mehreren Feldern

Sie können mehr als eine Art Schlüssel, zB sortiert nach zählen und wenn Zählungen gleich nehmen url berücksichtigt. Sie können dies tun, indem sie mit mehreren Schlüsseln mit der Reihenfolge ihrer Bedeutung wie so die Dekoration:

$arr = array_map(create_function('$a', 'return array($a["count"], $a["name"], $a);'), $arr); 

Warum es schnell

Auf diese Weise könnte schneller sein, dass usort verwenden, weil es Ihren benutzerdefinierten Code nur n-mal für die Sortierung Array der Länge n bezeichnet. Vergleiche während Art verwenden in Komparator gebaut getan, sollte schnell sein. In usort Verfahren benutzerdefinierte Komparator mehrere Male (mehr als n-mal) während der Art und es kann verlangsamen Dinge genannt.

function sortByCount($a, $b)
{
    if ($a['count'] == $b['count']) {
        return 0;
    }
    return ($a['count'] < $b['count']) ? -1 : 1;
}

usort($myarray, "sortByCount");
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top