sort PHP multidimensional array

I have the following array returned from a webservice (in the image I identify what I Want, below the text to copy paste if necessary): insert the description of the image here

Array
(
    [DataTable] => Array
        (
            [ID] => STOCK
            [Line] => Array
                (
                    [0] => Array
                        (
                            [Fields] => Array
                                (
                                    [0] => Array
                                        (
                                            [FieldID] => ItemCode
                                            [Value] => 1GADEME010001
                                        )

                                    [1] => Array
                                        (
                                            [FieldID] => ItemName
                                            [Value] => Sucata de ferro
                                        )

                                    [2] => Array
                                        (
                                            [FieldID] => FrgnName
                                            [Value] => Array
                                                (
                                                )

                                        )

                                    [3] => Array
                                        (
                                            [FieldID] => ItmsGrpCod
                                            [Value] => 112
                                        )
                    [0] => Array
                        (
                            [Fields] => Array
                                (
                                    [0] => Array
                                        (
                                            [FieldID] => ItemCode
                                            [Value] => 1GADEME010001
                                        )

                                    [1] => Array
                                        (
                                            [FieldID] => ItemName
                                            [Value] => Armário de ferro
                                        )

                                    [2] => Array
                                        (
                                            [FieldID] => FrgnName
                                            [Value] => Array
                                                (
                                                )

                                        )

                                    [3] => Array
                                        (
                                            [FieldID] => ItmsGrpCod
                                            [Value] => 112
                                        )

Given that the array has n records, I want to sort the array by name, which is underlined in red. I tried using https://stackoverflow.com/questions/2699086/sort-multi-dimensional-array-by-value but to no avail.

Author: Comunidade, 2016-04-27

3 answers

Well, I think I would do as follows. Just use the function usort.

usort($dados['dataTable']['lista'], function ($a, $b){ 
  return strcmp($a['fields'][0]['name'], $b['fields'][0]['name']); 
});

The function usort has the purpose of ordering a array according to a callback passed by the user (the programmer, in this case, hehehe).

The Callback must contain two parameters: these are the items, passed two by two, during the internal iteration that PHP will do.

According to the comparison is that is made there in the callback is that the array will be ordered.

You should Return 1, -1 or 0

Value 1-when you want the Element $a to be the first relative to $b

Value 0-when the sorting position remains the same.

Value -1 When the sorting position of $b should be greater than $a.

So why did I use strcmp in return?

This function, according to the manual :

Returns 0 if str1 is greater than str2, and 0 if they are equal.

Then, look at the following values to get an idea of our example cited above:

strcmp('a', 'b'); // int(-1)

strcmp('b', 'a'); // int(1)

strcmp('b', 'b'); // int(0)
 5
Author: Wallace Maxters, 2016-04-27 19:38:55

The logic for assembling order this type of structure is to assemble a simple array that will be ordered, keeping the association keys.

$dados = array( // DADOS TESTE
    'dataTable' => array(
        'lista' => array(
            0 => array(
                'fields' => array(
                    0 => array(
                        'name' => 'B'
                    )
                )
            ),
            1 => array(
                'fields' => array(
                    0 => array(
                        'name' => 'D'
                    )
                )
            ),
            2 => array(
                'fields' => array(
                    0 => array(
                        'name' => 'C'
                    )
                )
            ),
            3 => array(
                'fields' => array(
                    0 => array(
                        'name' => 'A'
                    )
                )
            ),
        )
    )
);

$arraySort = array(); // ARRAY MONO DIMENCIONAL QUE VAI SER ORDENADO 
foreach ($dados['dataTable']['lista'] as $k => $list){
    $arraySort[$k] = $list['fields'][0]['name']; // SET AO DADO RELEVANTE PARA A ORDENAÇÃO
}
asort($arraySort); // ORDENA MANTENDO AS CHAVES

$listaDados = array(); // ARRAY QUE IRA COPIAR "dados", MAS ORDENADO
foreach ($arraySort as $k => $value){
    $listaDados[] = $dados['dataTable']['lista'][$k];   // COPIA OS DADOS DO $k INDICADO
}

$dados['dataTable']['lista'] = $listaDados;

var_dump($dados);
 3
Author: Guilherme Lautert, 2016-04-27 18:56:48

I believe you can create a simple algorithm for sorting yourself, you can even rely on Bubble Sort.

For this, use a foreach that traverses all the keys in the array:

foreach ($array as $chave => $valor) {}

Note: this may be the hardest mode, but sometimes creating your own methods instead of always using ready-made functions helps you develop your logical thinking.

 0
Author: Douglas D. Carvalho, 2016-04-27 16:15:51