
Originally Posted by
stereofrog
Using usort() on the following array:
Code:
Array (array(5)) {
| ['0'] (array(3)) {
| | ['prod_name'] (string) = "card 12"
| | ['prod_type'] (string) = "1"
| | ['prod_id'] (string) = "17"
| }
| ['1'] (array(3)) {
| | ['prod_name'] (string) = "card 13"
| | ['prod_type'] (string) = "1"
| | ['prod_id'] (string) = "18"
| }
| ['2'] (array(3)) {
| | ['prod_name'] (string) = "Niec xmas card"
| | ['prod_type'] (string) = "1"
| | ['prod_id'] (string) = "26"
| }
| ['3'] (array(3)) {
| | ['prod_name'] (string) = "test xmas gift"
| | ['prod_type'] (string) = "2"
| | ['prod_id'] (string) = "1"
| }
| ['4'] (array(3)) {
| | ['prod_name'] (string) = "a very nice gift"
| | ['prod_type'] (string) = "2"
| | ['prod_id'] (string) = "2"
| }
}
using the following code:
Code:
function prod_cmp($a, $b)
{
return strcmp($a["prod_name"], $b["prod_name"]);
}
usort($_arrProducts, "prod_cmp");
produces the following results
Code:
Array (array(5)) {
| ['0'] (array(3)) {
| | ['prod_name'] (string) = "Niec xmas card"
| | ['prod_type'] (string) = "1"
| | ['prod_id'] (string) = "26"
| }
| ['1'] (array(3)) {
| | ['prod_name'] (string) = "a very nice gift"
| | ['prod_type'] (string) = "2"
| | ['prod_id'] (string) = "2"
| }
| ['2'] (array(3)) {
| | ['prod_name'] (string) = "card 12"
| | ['prod_type'] (string) = "1"
| | ['prod_id'] (string) = "17"
| }
| ['3'] (array(3)) {
| | ['prod_name'] (string) = "card 13"
| | ['prod_type'] (string) = "1"
| | ['prod_id'] (string) = "18"
| }
| ['4'] (array(3)) {
| | ['prod_name'] (string) = "test xmas gift"
| | ['prod_type'] (string) = "2"
| | ['prod_id'] (string) = "1"
| }
}
Not quite what I expected.... 
I've tried uksort() too, with and without the prod_name as the key, neither works...
Cheers,
Terry
Bookmarks