sort array multidimensional php

4

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):

Array([DataTable]=>Array([ID]=>STOCK[Line]=>Array([0]=>Array([Fields]=>Array([0]=>Array([FieldID]=>ItemCode[Value]=>1GADEME010001)[1]=>Array([FieldID]=>ItemName[Value]=>Sucatadeferro)[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áriodeferro)[2]=>Array([FieldID]=>FrgnName[Value]=>Array())[3]=>Array([FieldID]=>ItmsGrpCod[Value]=>112)

Giventhatthearrayhasnrecords,Iwanttosortthearraybyitsname,whichisunderlinedinred.Itriedusing link but without success.

    
asked by anonymous 27.04.2016 / 17:54

3 answers

4

Well, I think I would do it this way. Just use the usort function.

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 should contain two parameters: These are the items, passed in pairs, during the internal iteration PHP will do.

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

You should return 1 , -1 or 0

Value 1 - When you want the $a element to be first relative to $b

Value 0 - When the sort position remains the same.

Value -1 When the order position of $b must be greater than $a .

So why did I use strcmp in return?

This function, according to Manual :

  

Returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal.

So, look at the following values to get an idea of our example quoted above:

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

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

strcmp('b', 'b'); // int(0)
    
27.04.2016 / 21:02
2

The logic to mount this type of structure is to assemble a simple array that will be sorted by 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);
    
27.04.2016 / 20:56
0

I believe that you can create a simple algorithm for sorting yourself, it can even be based on Bubble Sort .

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

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

Note: This is probably the hardest way, but sometimes creating your own methods instead of always using ready-made functions helps you develop your logical thinking.

    
27.04.2016 / 18:15