How to sort an Array Array alphabetically in PHP?

1

I have an array nxm that I will call $tabela .

This array has some data inside it organized as follows:

| Papel1 | Hora1 | Valor1 | Erro  | B |
| Papel2 | Hora2 | Valor2 | OK_Ok | A |
| Papel3 | Hora3 | Valor3 | Falha | C |
| Papel4 | Hora4 | Valor4 | OK_OK | A |

All table data is extracted from a mysql table, except the last column that I enter in the array based on some conditions.

My question is, how to organize alphabetically all the rows of the array based on the last column? It would have to return this way:

| Papel2 | Hora2 | Valor2 | OK_Ok | A |
| Papel4 | Hora4 | Valor4 | OK_OK | A |
| Papel1 | Hora1 | Valor1 | Erro  | B |
| Papel3 | Hora3 | Valor3 | Falha | C |
Array ( [0] => Array ( [0] => MIL EUA [1] => 2018-03-05 14:00:13 [3] => 168.99 [2] => 2018-03-05 15:00:11 [4] => 168.99 [5] => Atraso entre os servidores de 59 minutos ) [1] => Array ( [0] => TRIGQQNP [1] => 2018-03-05 14:00:15 [3] => 3800.00 [2] => 2018-03-05 15:00:12 [4] => 3800.00 [5] => Atraso entre os servidores de 59 minutos ) )
    
asked by anonymous 05.03.2018 / 18:15

2 answers

1

You can use usort()

function cmp($a, $b)
{
    return strcmp($a["nome_ultimo_campo"], $b["nome_ultimo_campo"]);
}

usort($tabela, "cmp");

In this question has been raised something like what you need.

    
05.03.2018 / 18:25
2

If they are columns that came from a database like you stated in:

  

All table data is extracted from a mysql table

So before inserting into the array you could already bring ordinates using ORDER by ( ASC to crescent and DESC to non-believer), something like:

SELECT id, nome, foo, bar, ultimacoluna
FROM tabela
WHERE <condica>
LIMIT <limite>
ORDER by ultimacoluna ASC

So in the while or foreach that you are probably using to add the items to the new array they will already be in alphabetical order (depending on / varying the encoding system).

However if the problem is another I recommend you explain the behavior of the code and show an example of the array using print_r or var_dump so we can actually see what your true array format is for only so we can suggest the most efficient way for your specific case.

    
05.03.2018 / 18:39