You can't pass in additional arguments to this function as it's a callback of uasort(), but you could use global variables. See the example below. I've added two new variables, sort order and sort key. I've also added quantity to the product array as a new key to sort on.
<?php
// Setup product array (You already have yours)
$products = array(
'Bike' => array('price' => '199.99','quantity' => '1'),
'Apple' => array('price' => '0.87','quantity' => '2'),
'Car' => array('price' => '5999.00','quantity' => '3')
);
// *NEW* Specify either ASC or DESC
$sortorder = 'DESC';
// *NEW* Specify the key you'd like the array sorted on
$sortkey = 'quantity';
// Comparison function *NOTE* You now change the sort order with the global variable, not by swaping the return values around
function compare($x, $y){
global $sortorder;
global $sortkey;
if ( $x[$sortkey] == $y[$sortkey] ){
$return = 0;
}
if ($sortorder == 'ASC'){
return ($x[$sortkey] < $y[$sortkey]) ? -1 : 1;
}
else {
return ($x[$sortkey] < $y[$sortkey]) ? 1 : -1;
}
}
uasort($products, 'compare');
// Output the products
echo '<pre>';
print_r($products);
echo '</pre>';
?>