I have the following array:
$optionen_kosten = array(
"option_5" => array(
'monthly' => 2,
'setup' => 5
),
"option_3" => array(
'monthly' => 2,
'setup' => 5
),
"option_1" => array(
'monthly' => 2,
'setup' => 5
),
"option_6" => array(
'monthly' => 2,
'setup' => 5
),
"option_8" => array(
'monthly' => 2,
'setup' => 5
),
"option_10" => array(
'monthly' => 2,
'setup' => 5
),
"option_2" => array(
'monthly' => 2,
'setup' => 5
),
"option_4" => array(
'monthly' => 2,
'setup' => 5
),
"option_7" => array(
'monthly' => 2,
'setup' => 5
),
"option_9" => array(
'monthly' => 2,
'setup' => 5
)
);
Now, I want to sort the key of that array ascending (option_1
, option_2
...)
I tried it with ksort but it does not work perfectly:
option_1
option_10
option_2
option_3
option_4
option_5
option_6
option_7
option_8
option_9
I want option_10
to be the last one. Maybe with uksort?
PHP Version: 5.3.3
You want "natural sort" and you can add that to ksort
directly (in php 5.4+):
ksort($optionen_kosten, SORT_NATURAL);
var_dump($optionen_kosten);
See the manual on sort()
for more details on the optional parameter values.
For an older version of php you could use uksort()
.
That would be something like:
uksort($optionen_kosten, function($a, $b) {
return strnatcmp($a, $b);
});
var_dump($optionen_kosten);