Sorting with multidimensional array

Asked

Viewed 57 times

1

I have the following array():

$retorno[] = array(                 
"mes"=>$r1[3],
"ano"=>$r1[4],
"valor"=>$r1[2],
"codsubcategoria"=>$r1[6]
);

That in the var_dump() I have the following return, for example:

[0]=>
  array(4) {
    ["mes"]=>
    string(1) "9"
    ["ano"]=>
    string(4) "2019"
    ["valor"]=>
    string(5) "30.75"
    ["codsubcategoria"]=>
    string(1) "4"
  }
  [1]=>
  array(4) {
    ["mes"]=>
    string(2) "10"
    ["ano"]=>
    string(4) "2019"
    ["valor"]=>
    string(5) "30.75"
    ["codsubcategoria"]=>
    string(1) "4"
  }
  [2]=>
  array(4) {
    ["mes"]=>
    string(2) "10"
    ["ano"]=>
    string(4) "2019"
    ["valor"]=>
    string(6) "325.00"
    ["codsubcategoria"]=>
    string(1) "5"
  }
  [3]=>
  array(4) {
    ["mes"]=>
    string(1) "9"
    ["ano"]=>
    string(4) "2019"
    ["valor"]=>
    string(6) "325.00"
    ["codsubcategoria"]=>
    string(1) "5"
  }
  [4]=>
  array(4) {
    ["mes"]=>
    string(1) "9"
    ["ano"]=>
    string(4) "2019"
    ["valor"]=>
    string(5) "23.00"
    ["codsubcategoria"]=>
    string(1) "3"
  }

Note that in the key "month" there are months of September and October mixed. I wanted to order this return according to year and month. I tried with asort(), rsort(), but it didn’t work.

If you can help me.

2 answers

0

Leozera, for being multidimensional array you need to use the function array_multisort() it sorts multiple arrays or multidimensional arrays.

The sort to be used in the previous array argument. Can be SORT_ASC to sort in ascending (ascending) SORT_DESC to sort in descending (descending).

Below you can carry out the following way for you to carry out the precise:

<?php
$sortArray = array();
    foreach($retorno as $row){
        foreach($row as $key=>$value){ 
            if(!isset($sortArray[$key])){ 
                $sortArray[$key] = array(); 
            } 
                $sortArray[$key][] = $value;
        }
    } 

                array_multisort($sortArray['ano'],SORT_ASC,$sortArray['mes'],SORT_ASC, $retorno);

I hope I’ve helped

  • 1

    thanks, it worked out was what I needed. Thank you

0

You can sort this using the function usort. Just create a comparison function to pass as a parameter to the function usort.

function sortByOrder($a, $b) {
    return $a['mes'] - $b['mes'];
}

// Passa o seu array e a função de comparação como callback
usort($retorno, 'sortByOrder');

print_r($retorno);

Upshot:

Array
(
    [0] => Array
        (
            [mes] => 9
            [ano] => 2019
            [valor] => 30.75
            [codsubcategoria] => 4
        )

    [1] => Array
        (
            [mes] => 9
            [ano] => 2019
            [valor] => 325.00
            [codsubcategoria] => 5
        )

    [2] => Array
        (
            [mes] => 9
            [ano] => 2019
            [valor] => 23.00
            [codsubcategoria] => 3
        )

    [3] => Array
        (
            [mes] => 10
            [ano] => 2019
            [valor] => 30.75
            [codsubcategoria] => 4
        )

    [4] => Array
        (
            [mes] => 10
            [ano] => 2019
            [valor] => 325.00
            [codsubcategoria] => 5
        )

)
  • 1

    Kayo, it didn’t work ordered the month but the year got wrong, I needed the month and the year ordered

  • 1

    It would have to adapt this function to work with month and year?

  • It’s true, I let it go that I needed to sort through the year too, but I can adapt, once I get home, I make this adjustment and edit the answer.

Browser other questions tagged

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