# Sorting a multidimensional array by two values

I'm trying to sort a multidimensional array, firstly by 'sort_order' and then secondly by 'qty'. I've currently got the following, but the last sort is overriding it.

Here's my array:

```
Array
(
[0] => Array
(
[products] => Array
(
[product_id] => 36
[name] => Securitex PF - Latex Examination Glove (Carton - 1000pcs)
[model] => XS
[sku] => 40080
[price] => $110.17
[special] =>
[minimum] => 1
[sort_order] => 6
)
[category] => Shop by Carton
[text] => One Carton: 1000 pieces (10 boxes of 100)
[discount] => Buy 10 cartons and save another 10%
[qty] => 10
[type] => Box
)
[1] => Array
(
[products] => Array
(
[product_id] => 37
[name] => Securitex PF - Latex Examination Glove (Carton - 1000pcs)
[model] => S
[sku] => 41004
[price] => $110.17
[special] =>
[minimum] => 1
[sort_order] => 7
)
[category] => Shop by Carton
[text] => One Carton: 1000 pieces (10 boxes of 100)
[discount] => Buy 10 cartons and save another 10%
[qty] => 10
[type] => Box
)
[2] => Array
(
[products] => Array
(
[product_id] => 38
[name] => Securitex PF - Latex Examination Glove (Carton - 1000pcs)
[model] => M
[sku] => 41005
[price] => $110.17
[special] =>
[minimum] => 1
[sort_order] => 8
)
[category] => Shop by Carton
[text] => One Carton: 1000 pieces (10 boxes of 100)
[discount] => Buy 10 cartons and save another 10%
[qty] => 10
[type] => Box
)
[3] => Array
(
[products] => Array
(
[product_id] => 39
[name] => Securitex PF - Latex Examination Glove (Carton - 1000pcs)
[model] => L
[sku] => 41006
[price] => $110.17
[special] =>
[minimum] => 1
[sort_order] => 9
)
[category] => Shop by Carton
[text] => One Carton: 1000 pieces (10 boxes of 100)
[discount] => Buy 10 cartons and save another 10%
[qty] => 10
[type] => Box
)
[4] => Array
(
[products] => Array
(
[product_id] => 31
[name] => Securitex PF - Latex Examination Glove (Box - 100pcs)
[model] => XS
[sku] => 40080I
[price] => $13.23
[special] =>
[minimum] => 1
[sort_order] => 1
)
[category] => Shop by Box
[text] => One Box: 100 pieces
[discount] =>
[qty] => 1
[type] => Box
)
[5] => Array
(
[products] => Array
(
[product_id] => 32
[name] => Securitex PF - Latex Examination Glove (Box - 100pcs)
[model] => S
[sku] => 41004I
[price] => $13.23
[special] =>
[minimum] => 1
[sort_order] => 2
)
[category] => Shop by Box
[text] => One Box: 100 pieces
[discount] =>
[qty] => 1
[type] => Box
)
[6] => Array
(
[products] => Array
(
[product_id] => 33
[name] => Securitex PF - Latex Examination Glove (Box - 100pcs)
[model] => M
[sku] => 41005I
[price] => $13.23
[special] =>
[minimum] => 1
[sort_order] => 3
)
[category] => Shop by Box
[text] => One Box: 100 pieces
[discount] =>
[qty] => 1
[type] => Box
)
[7] => Array
(
[products] => Array
(
[product_id] => 34
[name] => Securitex PF - Latex Examination Glove (Box - 100pcs)
[model] => L
[sku] => 41006I
[price] => $13.23
[special] =>
[minimum] => 1
[sort_order] => 4
)
[category] => Shop by Box
[text] => One Box: 100 pieces
[discount] =>
[qty] => 1
[type] => Box
)
[8] => Array
(
[products] => Array
(
[product_id] => 35
[name] => Securitex PF - Latex Examination Glove (Box - 100pcs)
[model] => XL
[sku] => 41007I
[price] => $13.23
[special] =>
[minimum] => 1
[sort_order] => 5
)
[category] => Shop by Box
[text] => One Box: 100 pieces
[discount] =>
[qty] => 1
[type] => Box
)
)
```

The 'sort_order' is inside 'products', which is my first ordering. Which the above array is outputting.

The 'qty' is the second set of ordering, but the following code overrides my sort_order ordering.

```
function array_sort($array, $on, $order=SORT_ASC){
$new_array = array();
$sortable_array = array();
if (count($array) > 0) {
foreach ($array as $k => $v) {
if (is_array($v)) {
foreach ($v as $k2 => $v2) {
if ($k2 == $on) {
$sortable_array[$k] = $v2;
}
}
} else {
$sortable_array[$k] = $v;
}
}
switch ($order) {
case SORT_ASC:
asort($sortable_array);
break;
case SORT_DESC:
arsort($sortable_array);
break;
}
foreach ($sortable_array as $k => $v) {
$new_array[$k] = $array[$k];
}
}
return $new_array;
}
```

Which gives me this array (the majority are in order, except for the last one).

```
Array
(
[1] => Array
(
[category] => Shop by Box
[text] => One Box: 100 pieces
[discount] =>
[qty] => 1
[type] => Box
[products] => Array
(
[0] => Array
(
[product_id] => 31
[name] => Securitex PF - Latex Examination Glove (Box - 100pcs)
[model] => XS
[sku] => 40080I
[price] => $13.23
[special] =>
[minimum] => 1
[sort_order] => 1
)
[1] => Box
[2] => Array
(
[product_id] => 32
[name] => Securitex PF - Latex Examination Glove (Box - 100pcs)
[model] => S
[sku] => 41004I
[price] => $13.23
[special] =>
[minimum] => 1
[sort_order] => 2
)
[3] => 1
[4] => Array
(
[product_id] => 33
[name] => Securitex PF - Latex Examination Glove (Box - 100pcs)
[model] => M
[sku] => 41005I
[price] => $13.23
[special] =>
[minimum] => 1
[sort_order] => 3
)
[5] => Array
(
[product_id] => 34
[name] => Securitex PF - Latex Examination Glove (Box - 100pcs)
[model] => L
[sku] => 41006I
[price] => $13.23
[special] =>
[minimum] => 1
[sort_order] => 4
)
[6] => Array
(
[product_id] => 35
[name] => Securitex PF - Latex Examination Glove (Box - 100pcs)
[model] => XL
[sku] => 41007I
[price] => $13.23
[special] =>
[minimum] => 1
[sort_order] => 5
)
)
)
[0] => Array
(
[category] => Shop by Carton
[text] => One Carton: 1000 pieces (10 boxes of 100)
[discount] => Buy 10 cartons and save another 10%
[qty] => 10
[type] => Box
[products] => Array
(
[0] => Array
(
[product_id] => 37
[name] => Securitex PF - Latex Examination Glove (Carton - 1000pcs)
[model] => S
[sku] => 41004
[price] => $110.17
[special] =>
[minimum] => 1
[sort_order] => 7
)
[1] => Array
(
[product_id] => 38
[name] => Securitex PF - Latex Examination Glove (Carton - 1000pcs)
[model] => M
[sku] => 41005
[price] => $110.17
[special] =>
[minimum] => 1
[sort_order] => 8
)
[2] => Array
(
[product_id] => 39
[name] => Securitex PF - Latex Examination Glove (Carton - 1000pcs)
[model] => L
[sku] => 41006
[price] => $110.17
[special] =>
[minimum] => 1
[sort_order] => 9
)
[3] => Box
[4] => 10
[5] => Array
(
[product_id] => 36
[name] => Securitex PF - Latex Examination Glove (Carton - 1000pcs)
[model] => XS
[sku] => 40080
[price] => $110.17
[special] =>
[minimum] => 1
[sort_order] => 6
)
)
)
)
```