Sort array by itself within PHP Intel

Asked

Viewed 408 times

2

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

The returned array is a JSON of that (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.

I’m not finding the function that I can do this, well it’s that doubt:

How to sort an array when there is no input and the value is the proper of the second dimension ?

Example of how it would look if I received 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'
     ]
];

UPDATING

I got it on a whim, but I still think there’s got to be a nicer 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
  • You can give an example of how this data would look after such an order?

  • @Andersoncarloswoss put in the question there the example, I’m trying with array_multisort, but I still haven’t found the way

3 answers

3


With PHP 5.5.0 or higher, you can make use of the function array_multisort together with the 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 in 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 function array_column you will create the order of the values based on the desired column and this order will be kept in the order of the others arrays, which is the very $arr.

2

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

Basically it does the following, checks the index of the first of the ordered 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
        )

)

Array Final:

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
        )

)
  • 1

    So also for sure, it only has a lot of checks, the way that Anderson went is cleaner, but still this way also for sure!

  • Really, Anderson’s got better

1

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

Receiving the 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 defining 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 requested information:

array_multisort(array_column($records['data'], intval($col)), $order_arr['dir'], $records['data']);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.