这些都不起作用......(没有排序)
我从 PHP 文档站点上的一个示例中改编了这些内容。
class ProductHelper {
function sortProductsByPrice($products, $sort = SORT_ASC) {
foreach ($products as $key => $row) {
$name[$key] = $row['name'];
$rrp[$key] = $row['rrp'];
}
array_multisort($rrp, $sort, $name, SORT_ASC, $products);
}
function sortProductsByName($products, $sort = SORT_ASC) {
foreach ($products as $key => $row) {
$name[$key] = $row['name'];
}
array_multisort($name, $sort, $products);
}
}
这就是我使用它的方式:
$products = $cur_prod_cat["products"]; // copy an array of products
$PRODUCT_HELPER->sortProductsByName($products); //sort it
如果您需要查看, products 数组看起来像这样:
Array (
[0] => Array (
[id] => 0
[name] => product name
[description] => product description
[price] => product price
[etc] => other attributes
)
[1] => Array (
[id] => 1
[name] => product name
[description] => product description
[price] => product price
[etc] => other attributes
)
)