I have the following array:
$data['array'] = array(
1 => array(
'currency_name' => 'USD',
'totals' => '310.00 USD'
),
24 => array(
'currency_name' => 'EUR',
'totals' => '200.00 EUR'
),
26 => array(
'currency_name' => 'GBP',
'totals' => '100.00 GBP'
)
);
I wanted to be sorted by currency_name
key and I used the following function:
// sort the array by currency_name key
$sort = array();
foreach ($data['array'] as $i => $row)
{
$sort[$i] = $row['currency_name'];
}
array_multisort($sort, SORT_NATURAL, $data['array']);
Output:
Array
(
[array] => Array
(
[0] => Array
(
[currency_name] => EUR
[totals] => 200.00 EUR
)
[1] => Array
(
[currency_name] => GBP
[totals] => 100.00 GBP
)
[2] => Array
(
[currency_name] => USD
[totals] => 310.00 USD
)
)
)
Expected:
Array
(
[array] => Array
(
[24] => Array
(
[currency_name] => EUR
[totals] => 200.00 EUR
)
[26] => Array
(
[currency_name] => GBP
[totals] => 100.00 GBP
)
[1] => Array
(
[currency_name] => USD
[totals] => 310.00 USD
)
)
)
This is reindexing the array, which I don't want. I need those keys later on.
Note:
* The method I've used above was this
* I need SORT_NATURAL
as I use this function for other strings too.