Monday, August 2, 2010

How to sort an two-dimensional array with respect to an element

$item_arr=Array
(
[0] => Array
(
[resourceid] => 854
[refno] => 268475
[fax] => +61 2 6198 3333
[phone] => +61 2 6198 3300
)

[1] => Array
(
[resourceid] => 750
[refno] => 268476
[fax] => +61 8 8233 5858
[phone] => +61 8 8233 5888
)

[2] => Array
(
[resourceid] => 675
[refno] => 268477
[fax] => +91 40 4033 9999
[phone] => +91 40 4033 9900
)

[3] => Array
(
[resourceid] => 522
[refno] => 268478
[fax] => +86 10 8520 0110
[phone] => +86 10 8520 0000
)

[4] => Array
(
[resourceid] => 647
[refno] => 268479
[fax] => +86 21 6103 7070
[phone] => +86 21 6103 7000
)

)
to sort above array with respect to refno,

function sorter($a, $b) { return $a["refno"] > $b["refno"]; }
usort($item_arr,"sorter");

No comments:

Post a Comment