select sale_context, delivery_method, payment_method FROM sales order by 1, 2, 3
this query will output data ordered by sale_context then by delivery_method then by payment_method.
now I want to do the same thing with arrays in php,
I let my code explain how to do it
$data = array(); $data[] = array('sale_context' => 'internet', 'delivery_method' => 'regular post', 'payment_method' => 'visa'); $data[] = array('sale_context' => 'phone', 'delivery_method' => 'express post', 'payment_method' => 'mastercard'); $data[] = array('sale_context' => 'internet', 'delivery_method' => 'regular post', 'payment_method' => 'cach'); $data[] = array('sale_context' => 'phone', 'delivery_method' => 'express post', 'payment_method' => 'visa'); // order by sale_context, delivery_method, payment_method $sales_context_list = array(); $delivery_method_list = array(); $payment_method_list = array(); foreach($data AS $key => $row) { $sales_context_list[] = $row['sale_context']; $delivery_method_list[] = $row['delivery_method']; $payment_method_list[] = $row['payment_method']; } echo "DATA BEFORE SORT"; print_r($data); array_multisort($sales_context_list, SORT_ASC, $delivery_method_list, SORT_ASC, $payment_method_list, SORT_ASC, $data); echo "DATA AFTER order by sale_context, delivery_method, payment_method "; print_r($data);
No comments:
Post a Comment