4
I have an array with two columns, where in the first column I have the name of a station, and in the second I have the address of the same.
I need to sort this array alphabetically by station name, without losing the associated address!
I tried the function array_multisort()
, but I can only sort the columns separately. You can help me?
This was the code used, based on the function documentation array_multisort()
. It returns the "$data" vector still out of order, and the "$sites" and "$Ips" vectors are empty.
$data = array( 'sites' => $lista_estacoes, 'IPs' => $lista_enderecos);
foreach ($data as $key => $row) {
$sites[$key] = $row['sites'];
$IPs[$key] = $row['IPs'];
}
array_multisort($sites, SORT_DESC, $IPs, SORT_ASC, $data);
Where "$lista_estacoes
" and "$lista_enderecos
" are the vectors containing the data of the stations. Some error I am not noticing?
The array "$data
" is like this:
Array
(
[sites] => Array
(
[0] =>
[1] => PrimeiraEstacao
[2] => SegundaEstacao
[3] => TerceiraEstacao
[4] =>
[5] => QuintaEstacao
...
)
[IPs] => Array
(
[0] =>
[1] => 172.168.0.11
[2] => 172.168.0.12
[3] => 172.168.0.13
[4] => 172.168.0.14
[5] => 172.168.0.15
...
)
)
Face a glance at the documentation of the array_multisort I believe that Example #3 Sorting database Results is your case
– Adir Kuhn
@Adirkuhn, thank you! I edited the question by adding the code made based on the documentation, which does not yet do the ordering. Can you find a mistake?
– Geraldo Peres
shows how this is the contents of your array
– Adir Kuhn
@Does Adirkuhn have 730 lines, something specific in the composition of it? Analyzing it, I found some null values in the 'sites' column, this can interfere with the function?
– Geraldo Peres
Not in theory, but put a small piece of that array to see its structure
– Adir Kuhn
@Adirkuhn, edited with a piece of the array.
– Geraldo Peres