I have data like the following,
Bale # | Factory # | Design | Color | Price |
----------------------------------------------------------------
1 | PDX-1 | D1 | RED | 10 |
1 | PDX-10 | D2 | BLUE | 200 |
1 | PDX-2 | D3 | PINK | Some int|
1 | PDX-3 | D1 | WHITE | Some int|
2 | PDX-4 | D3 | APPLE | Some int|
2 | PXX-56 | D3 | PINE | Some int|
2 | XXX-1 | D1 | SILVER | Some int|
1 | XXX-4 | D5 | BROWN | Some int|
1 | DFX-1 | D5 | COFFEE | Some int|
3 | ABC-1 | D6 | PURPLE | Some int|
1 | ABC-2 | D6 | GOLD | Some int|
This is in multi dimensional array. Where I put the Bale# in key, and other values in sub array against the bale.
forloop (...)
(.....)
$sorted_by_bale[$BALE_NO][] = array(
'jnb' => $factory_number,
'design_name' => $order_design,
'colorway' => $order_colorway,
'usd_rate' => $price,
);
}
I require to sort the values order by bale, and then telling the total price for one bale and number of items in a bale.
ksort($sorted_by_bale);
Ksort served the purpose.
Now I need to sort by Design (first) and then Color (second) within bale.
You should use usort
(see documentation). This allows you to use your own sort function.
One downside is that the array keys are reset. But this does not seem to matter to your secondary arrays.
function sortBale($first, $second)
{
if($first['design_name'] > $second['design_name']) {
return 1;
} elseif( $first['design_name'] < $second['design_name'] ) {
return -1;
} else {
//they have the same design
if($first['colorway'] > $second['colorway']) {
return 1;
} elseif( $first['colorway'] < $second['colorway'] ) {
return -1;
} else {
return 0;
}
}
}