Sort array by itself within the index in PHP

2

I'm working with the datatables, and by chance I decided to use the function ajax of it, I did the whole process, but one thing is missing, I can reorder the array, because using processing server-side dataTables does not interact client-side to reorder.

The returned array is a JSON of this (here it is in order, but I need to be able to change that order as I want):

$arr = [
     [
     'linha 1 coluna 1 valor z',
     'linha 1 coluna 2 valor b',
     'linha 1 coluna 3 valor h',
     'linha 1 coluna 4 valor d',
     'linha 1 coluna 5 valor e'
     ],    
     [
     'linha 2 coluna 1 valor b',
     'linha 2 coluna 2 valor c',
     'linha 2 coluna 3 valor r',
     'linha 2 coluna 4 valor i',
     'linha 2 coluna 5 valor l'
     ],    
     [
     'linha 3 coluna 1 valor q',
     'linha 3 coluna 2 valor w',
     'linha 3 coluna 3 valor y',
     'linha 3 coluna 4 valor u',
     'linha 3 coluna 5 valor s'
     ]
];

What I need is to be able to sort the row by the value of any of the columns based on the value it has itself.

I am not able to find the function that I can do this, well it is this doubt:

  

How to sort an array when there is no index and the value is its own   index of the second dimension?

Example of what it would look like if I were given the command to sort by column 1:

$arr = [ 
     [
     'linha 2 coluna 1 valor b',
     'linha 2 coluna 2 valor c',
     'linha 2 coluna 3 valor r',
     'linha 2 coluna 4 valor i',
     'linha 2 coluna 5 valor l'
     ],     
     [
     'linha 3 coluna 1 valor q',
     'linha 3 coluna 2 valor w',
     'linha 3 coluna 3 valor y',
     'linha 3 coluna 4 valor u',
     'linha 3 coluna 5 valor s'
     ],
     [
     'linha 1 coluna 1 valor z',
     'linha 1 coluna 2 valor b',
     'linha 1 coluna 3 valor h',
     'linha 1 coluna 4 valor d',
     'linha 1 coluna 5 valor e'
     ]
];
  

UPDATE

I was able to play a game, but I still think there should be a better way:

$ ordergin = $ var ['order_rule'];

foreach ($records['data'] as $key => $row) {
    $order_company[$key] = $row[1];
    $order_cnpj[$key] = $row[2];
    $order_tax_regime[$key] = $row[3];
    $order_responsible[$key] = $row[3];
    $order_status[$key] = $row[5];
}

if ($ordering == 'order_by_resp_desc') {
    array_multisort($order_responsible, SORT_DESC, $records['data']);
} elseif($ordering == 'order_by_resp_asc'){
    array_multisort($order_responsible, SORT_ASC, $records['data']);
} 
//Seguindo daqui até todas as opções disponiveis de ordenamento
    
asked by anonymous 19.10.2017 / 19:06

3 answers

3

With PHP 5.5.0 or higher, you can make use of array_multisort in conjunction with array_column :

<?php

$arr = [['z', 'b', 'h', 'd', 'e'], ['b', 'c', 'r', 'i', 'l'], ['q', 'w', 'y', 'u', 's']];

// Ordena $arr pelo valor da coluna 0:
array_multisort(array_column($arr, 0), SORT_ASC, $arr);

print_r($arr);

See working at Ideone | Repl.it

Resulting in:

Array
(
    [0] => Array
        (
            [0] => b
            [1] => c
            [2] => r
            [3] => i
            [4] => l
        )

    [1] => Array
        (
            [0] => q
            [1] => w
            [2] => y
            [3] => u
            [4] => s
        )

    [2] => Array
        (
            [0] => z
            [1] => b
            [2] => h
            [3] => d
            [4] => e
        )

)

With the array_column function you will create the order of the values based on the desired column and this order will be kept in the ordering of the other arrays , which is itself $arr . >     

19.10.2017 / 19:59
2

I made a small example, I used the arsort function that already exists in php just to exemplify, but you can choose the one you want, this code will work if the ordering also sorts the indexes.

Basically it does the following, checks the index of the first of the sorted array and sorts the others in those positions:

<?php

echo "<pre>";

$arr = [
    [
        'linha 1 coluna 1 valor a',
        'linha 1 coluna 2 valor b',
        'linha 1 coluna 3 valor c',
        'linha 1 coluna 4 valor d',
        'linha 1 coluna 5 valor e'
    ],
    [
        'linha 2 coluna 1 valor f',
        'linha 2 coluna 2 valor g',
        'linha 2 coluna 3 valor h',
        'linha 2 coluna 4 valor i',
        'linha 2 coluna 5 valor j'
    ],
    [
        'linha 3 coluna 1 valor k',
        'linha 3 coluna 2 valor l',
        'linha 3 coluna 3 valor m',
        'linha 3 coluna 4 valor n',
        'linha 3 coluna 5 valor o'
    ]
];

print_r($arr);

//Indice que vai ser ordenado
$indiceOrdenado = 0;

//Função de ordenação
arsort($arr[$indiceOrdenado]);

//Ordena os outros indices de acordo com o indice ordenado
foreach ($arr as $index => $item) {
    if ($index == $indiceOrdenado) {
        continue;
    }
    $i = 0;
    reset($arr[$indiceOrdenado]);
    $temp = $arr[$index][key($arr[$indiceOrdenado])];
    foreach ($arr[$indiceOrdenado] as $indice => $sub_item) {
        $arr[$index][$indice] = $arr[$index][$i];
        $i++;
    }
    $arr[$index][0] = $temp;
}

print_r($arr);

echo "</pre>";

Initial Array:

Array
(
    [0] => Array
        (
            [0] => linha 1 coluna 1 valor a
            [1] => linha 1 coluna 2 valor b
            [2] => linha 1 coluna 3 valor c
            [3] => linha 1 coluna 4 valor d
            [4] => linha 1 coluna 5 valor e
        )

    [1] => Array
        (
            [0] => linha 2 coluna 1 valor f
            [1] => linha 2 coluna 2 valor g
            [2] => linha 2 coluna 3 valor h
            [3] => linha 2 coluna 4 valor i
            [4] => linha 2 coluna 5 valor j
        )

    [2] => Array
        (
            [0] => linha 3 coluna 1 valor k
            [1] => linha 3 coluna 2 valor l
            [2] => linha 3 coluna 3 valor m
            [3] => linha 3 coluna 4 valor n
            [4] => linha 3 coluna 5 valor o
        )

)

Final Array:

Array
(
    [0] => Array
        (
            [4] => linha 1 coluna 5 valor e
            [3] => linha 1 coluna 4 valor d
            [2] => linha 1 coluna 3 valor c
            [1] => linha 1 coluna 2 valor b
            [0] => linha 1 coluna 1 valor a
        )

    [1] => Array
        (
            [0] => linha 2 coluna 5 valor j
            [1] => linha 2 coluna 2 valor g
            [2] => linha 2 coluna 3 valor h
            [3] => linha 2 coluna 2 valor g
            [4] => linha 2 coluna 1 valor f
        )

    [2] => Array
        (
            [0] => linha 3 coluna 5 valor o
            [1] => linha 3 coluna 2 valor l
            [2] => linha 3 coluna 3 valor m
            [3] => linha 3 coluna 2 valor l
            [4] => linha 3 coluna 1 valor k
        )

)
    
19.10.2017 / 19:44
1

Answering the question to tell you how you got the answers you received:

Receiving data (passed by DataTables), and calling a function to define:

 $order_col = $_POST['order'][0]['column'];
 $order_dir = $_POST['order'][0]['dir'];
 $order_arr = self::get_order_dir($order_dir);

Function that defines the order:

private static function get_order_dir($dir) {
    if ($dir == 'desc') {
        $rs['dir'] = SORT_DESC;
    } else {
        $rs['dir'] = SORT_ASC;
    }
    return $rs;
}

Doing the reordering with 'array_multisort' according to the requested index:

array_multisort(array_column($records['data'], intval($col)), $order_arr['dir'], $records['data']);
    
19.10.2017 / 20:32