1

i am new in php , i done't know the array sort. Problem is i need to sort array taht listed bellow to sort by its key key field order. means need it sort by '1,2,5'.

Array :

Array
(
    [Apple] => Array
        (
            [name] => Apparel & Accessories                 
            [order] => 5
        )

    [Bannana] => Array
        (
            [name] => Business Stationary
            [order] => 1
        )

    [Orenge] => Array
        (
            [name] => Business Cards
            [order] => 2
        )

)

How can i sort this array by Order ?

nee d to display like ::

Array
    (
        [Bannana] => Array
            (
                [name] => Business Stationary                   
                [order] => 1
            )

        [Orenge] => Array
            (
                [name] => Business Cards
                [order] => 2
            )

        [apple] => Array
            (
                [name] => Apparel & Accessories 
                [order] => 5
            )

    )
Mark Richards
  • 434
  • 1
  • 8
  • 24

3 Answers3

3

This should work for you:

<?php

    $array = array(
                "Apple" => array(
                        "name" => "Apparel & Accessories",                 
                        "order" => 5
                    ),

                "Bannana" => array(
                        "name" => "Business Stationary",
                        "order" => 1
                    ),

                "Orenge" => array (
                        "name" => "Business Cards",
                        "order" => 2
                    )

            );


    function sortByOrder($a, $b) {
        return $a['order'] - $b['order'];
    }

    usort($array, 'sortByOrder');   


    print_r($array);

?>

Output:

    Array
(
    [0] => Array
        (
            [name] => Business Stationary
            [order] => 1
        )

    [1] => Array
        (
            [name] => Business Cards
            [order] => 2
        )

    [2] => Array
        (
            [name] => Apparel & Accessories
            [order] => 5
        )

)
Rizier123
  • 58,877
  • 16
  • 101
  • 156
1

Suggest you to use usort(). Example:

$arr = array
(
    "Apple" => array
    (
        "name" => "Apparel & Accessories" ,
        "order" => 5
    ),

    "Bannana" => array
    (
        "name" => "Business Stationary",
        "order" => 1
    ),

    "Orenge" => array
    (
        "name" => "Business Cards",
        "order" => 2
    )

);


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

usort($arr, "custom_sort");

print '<pre>';
print_r($arr);
print '</pre>';
MH2K9
  • 11,951
  • 7
  • 32
  • 49
1

Usort function comes here!

function sortbyorder($a, $b) {
    if ($a['order'] > $b['order']) return true; 
    else return false;
}
usort ($array, 'sortbyorder');

More information here: http://php.net/manual/en/function.usort.php Best regards.

It can also be:

function sortbyorder($a, $b) {
    return $a['order'] > $b['order']; 
}
usort ($array, 'sortbyorder');
Jacek Kowalewski
  • 2,761
  • 2
  • 23
  • 36