Php iç içe array sıralama (Recursive array sorting)

Php ile recursive (iç içe) array sıralaması için ihtiyacınız olan fonksyion usort

usort — Bir diziyi kullanıcı tanımlı bir işlev kullanarak değerlerine göre sıralar

Bu işlev kullanıcı tarafından sağlanmış bir karşılaştırma işlevini kullanarak diziyi değerlerine göre sıralar. Sıralamak istediğiniz diziyi mevcut kuralların dışında bir kurala göre sıralamak isterseniz bu işlev çok işe yarar.

Örnek verimiz

$array = array(
array("url" => "https://deneme.com", "title" => "Example test title", "desc"=>"Example Test Desc", "type"=>"static"),
array("url" => "https://deneme.com", "title" => "Example test title", "desc"=>"Example Test Desc", "type"=>"dynamic"),
array("url" => "https://deneme.com", "title" => "Example test title", "desc"=>"Example Test Desc", "type"=>"static")
);

Type static olanları sonda, diğerlerini başta olmak üzere sıralayalım

usort($array, function($a, $b) {
    return $a["type"] === "static";
});
print_r($array);

//Sonuç

Array
(
    [0] => Array
        (
            [url] => https://deneme.com
            [title] => Example test title
            [desc] => Example Test Desc
            [type] => dynamic
        )
    [1] => Array
        (
            [url] => https://deneme.com
            [title] => Example test title
            [desc] => Example Test Desc
            [type] => static
        )
    [2] => Array
        (
            [url] => https://deneme.com
            [title] => Example test title
            [desc] => Example Test Desc
            [type] => static
        )
)

Type static olanları başta, diğerlerini sonda olmak üzere sıralayalım

usort($array, function($a, $b) {
    return $a["type"] !== "static";
});
print_r($array);

//Sonuç

Array
(
    [0] => Array
        (
            [url] => https://deneme.com
            [title] => Example test title
            [desc] => Example Test Desc
            [type] => static
        )
    [1] => Array
        (
            [url] => https://deneme.com
            [title] => Example test title
            [desc] => Example Test Desc
            [type] => static
        )
    [2] => Array
        (
            [url] => https://deneme.com
            [title] => Example test title
            [desc] => Example Test Desc
            [type] => dynamic
        )
)

Sayısl değerlere göre sıralamak

Artana göre sıralama

$array = array(
array("url" => "https://deneme.com", "title" => "Example test title", "desc"=>"Example Test Desc", "order"=>5),
array("url" => "https://deneme.com", "title" => "Example test title", "desc"=>"Example Test Desc", "order"=>2),
array("url" => "https://deneme.com", "title" => "Example test title", "desc"=>"Example Test Desc", "order"=>6)
);

usort($array, function($a, $b) {
    return $a["order"] > $b["order"];
});
print_r($array);

//Sonuç

Array
(
    [0] => Array
        (
            [url] => https://deneme.com
            [title] => Example test title
            [desc] => Example Test Desc
            [order] => 2
        )
    [1] => Array
        (
            [url] => https://deneme.com
            [title] => Example test title
            [desc] => Example Test Desc
            [order] => 5
        )
    [2] => Array
        (
            [url] => https://deneme.com
            [title] => Example test title
            [desc] => Example Test Desc
            [order] => 6
        )
)

Azalana göre sıralama

header('Content-Type: application/json');

$array = array(
array("url" => "https://deneme.com", "title" => "Example test title", "desc"=>"Example Test Desc", "order"=>5),
array("url" => "https://deneme.com", "title" => "Example test title", "desc"=>"Example Test Desc", "order"=>2),
array("url" => "https://deneme.com", "title" => "Example test title", "desc"=>"Example Test Desc", "order"=>6)
);

usort($array, function($a, $b) {
    return $a["order"] < $b["order"];
});
print_r($array);

//Sonuç

Array
(
    [0] => Array
        (
            [url] => https://deneme.com
            [title] => Example test title
            [desc] => Example Test Desc
            [order] => 6
        )
    [1] => Array
        (
            [url] => https://deneme.com
            [title] => Example test title
            [desc] => Example Test Desc
            [order] => 5
        )
    [2] => Array
        (
            [url] => https://deneme.com
            [title] => Example test title
            [desc] => Example Test Desc
            [order] => 2
        )
)