# Sort a multidimensional an array with numeric keys but keep the keys same just change the order

So I have 3 dimensional array. I want that array to be reordered based on the keys but the value of the keys should remain as it is. Like for an example if the array keys are 5,2,4,1,3 then it should become 1,2,3,4,5. Below I'm providing the array I have and excepted array and the solutions I have tried.

This is the array I have :-

``````[5] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => E3
[deal_text] =>
[units] => 5
[total_units] => 5
[amount] => 2620.8333333333
[is_freezed] =>
[can_sell] => 1
)

)

)
[2] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => E4
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 516.66666666667
[is_freezed] => 1
[can_sell] =>
)

)

)
[4] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => C8
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 526.66666666667
[is_freezed] => 1
[can_sell] =>
)
)

)
[1] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => D4
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 557.14285714286
[is_freezed] => 1
[can_sell] =>
)
)
)
[3] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => E5
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 516.66666666667
[is_freezed] => 1
[can_sell] =>
)

)

)
``````

Following are the solutions I have tried :-

``````\$result = ksort(\$result);
\$result = array_values(\$result);
\$result = array_splice(\$result, 0, 0);
\$result = sort(\$result);
\$result = array_splice(\$result, 0, count(\$result));
``````

This is the expected array :-

``````Array
(
[1] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => D4
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 557.14285714286
[is_freezed] => 1
[can_sell] =>
)
)
)

[2] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => E4
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 516.66666666667
[is_freezed] => 1
[can_sell] =>
)

)

)

[3] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => E5
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 516.66666666667
[is_freezed] => 1
[can_sell] =>
)

)

)

[4] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => C8
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 526.66666666667
[is_freezed] => 1
[can_sell] =>
)
)

)

[5] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => E3
[deal_text] =>
[units] => 5
[total_units] => 5
[amount] => 2620.8333333333
[is_freezed] =>
[can_sell] => 1
)

)

)

)
``````

Nothing is working any help will be appreciated. thanks in advance.

On Best Solutions

You are using `ksort` as `\$result = ksort(\$result);`, `ksort` return TRUE/FALSE. That means you are assigning that to `\$results`. Read here PHP ksort

``````ksort(\$results);
``````

``````\$result = ksort(\$result);
``````

You can use `ksort` for the keys sorting, here is an example

``````\$arr = [
5 => [1,3],
3 => [2,3],
2 => [0,7]
];
ksort(\$arr);
echo '<pre>';
print_r(\$arr);
``````

Output

``````Array
(
[2] => Array
(
[0] => 0
[1] => 7
)

[3] => Array
(
[0] => 2
[1] => 3
)

[5] => Array
(
[0] => 1
[1] => 3
)

)
``````