Tom Shaw wrote:
I'm having a problem sorting my array and wondered if anybody had experience
sorting arrays by their values. What I need to do is resort the array below
where the most expensive product shipping price starts at position zero no
matter how big the array is.
array(2) {
[0] => array(48) {
["product_id"] => string(2) "34"
["product_name"] => string(29) "Bears Ball Cap"
["product_ordered_size"] => string(5) "ADULT"
["product_sales_price"] => string(8) "11.90"
["product_shipping_price"] => string(4) "7.85"
["product_shipping_extra"] => string(4) "0.06"
}
[1] => array(48) {
["product_id"] => string(2) "37"
["product_name"] => string(21) "Baldwin L Grand Piano"
["product_ordered_size"] => string(5) "Grand"
["product_sales_price"] => string(8) "11671.90"
["product_shipping_price"] => string(6) "500.00"
["product_shipping_extra"] => string(6) "450.00"
}
Thanks
php.coder@xxxxxxxxx
Everything can be done with three custom lines of code, the rest is PHP
existing function calls.
http://us3.php.net/manual/en/function.array-multisort.php
Example #3 Sorting database results
I expanded your list below and randomly added some data... Anyways,
replace my $ar array definition with your array and you should be good
to go. One question though, is this coming from a DB? If so, why not
do the sorting in the SQL call instead of PHP? Just a thought.
<plaintext><?php
$ar = array(
array(
"product_id" => "34",
"product_name" => "Bears Ball Cap #34",
"product_ordered_size" => "ADULT",
"product_sales_price" => "11.90",
"product_shipping_price" => "7.85",
"product_shipping_extra" => "0.06",
),
array(
"product_id" => "35",
"product_name" => "Baldwin L Grand Piano #35",
"product_ordered_size" => "Grand",
"product_sales_price" => "11671.90",
"product_shipping_price" => "51.00",
"product_shipping_extra" => "450.00",
),
array(
"product_id" => "36",
"product_name" => "Baldwin L Grand Piano #36",
"product_ordered_size" => "Grand",
"product_sales_price" => "11671.90",
"product_shipping_price" => "500.00",
"product_shipping_extra" => "450.00",
),
array(
"product_id" => "37",
"product_name" => "Baldwin L Grand Piano #37",
"product_ordered_size" => "Grand",
"product_sales_price" => "11671.90",
"product_shipping_price" => "5000.00",
"product_shipping_extra" => "450.00",
),
array(
"product_id" => "38",
"product_name" => "Baldwin L Grand Piano #38",
"product_ordered_size" => "Grand",
"product_sales_price" => "11671.90",
"product_shipping_price" => "50.00",
"product_shipping_extra" => "450.00",
),
array(
"product_id" => "39",
"product_name" => "Baldwin L Grand Piano #39",
"product_ordered_size" => "Grand",
"product_sales_price" => "11671.90",
"product_shipping_price" => "1.00",
"product_shipping_extra" => "450.00",
)
);
foreach ( $ar AS $k => $v ) {
$p_shipping_price[$k] = $v['product_shipping_price'];
}
array_multisort($p_shipping_price, SORT_DESC, $ar);
var_dump($ar);
?>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php