How to sort an array with php

Asked

Viewed 1,374 times

1

Well I have a variable $produtoswhich receives the following array:

 array (size=6)
      0 => 
        array (size=4)
          'id' => string '107' (length=3)
          'nome' => string 'LAREIRA PEQ.' (length=12)
          'qtd' => float 43
          'total' => float 64500
      1 => 
        array (size=4)
          'id' => string '108' (length=3)
          'nome' => string 'CHORAQUERIA PEQ.' (length=16)
          'qtd' => float 60
          'total' => float 48000
      2 => 
        array (size=4)
          'id' => string '109' (length=3)
          'nome' => string 'JOGO DE FACAS' (length=13)
          'qtd' => float 90
          'total' => float 27000
      3 => 
        array (size=4)
          'id' => string '111' (length=3)
          'nome' => string 'PROVALEIRA' (length=10)
          'qtd' => float 100
          'total' => float 6000
      4 => 
        array (size=4)
          'id' => string '110' (length=3)
          'nome' => string 'COOLERS' (length=7)
          'qtd' => float 84
          'total' => float 21000
      5 => 
        array (size=4)
          'id' => string '112' (length=3)
          'nome' => string 'CHAMPANHEIRA' (length=12)
          'qtd' => float 28
          'total' => float 1962.64

Good as I do to sort it by field Qtd, or in alphabetical order?

I’m mounting the array like this:

while ($resultado = mysqli_fetch_object($consulta)) {

    // Array com dados do produto
    $curva[] = array(
        "id" => $resultado->id,
        "nome" => $resultado->nome,
        "qtd" => $qtd,
        "total" => $total
    );
}

I need to take the array and change its order. There are ways to do this?

  • 2

    You could have put the array in php instead of dump... think about the work people will have to test :(

  • I was just gonna say that @Guilhermenascimento

  • Good excuse, and that I am reading everything from the comic book, I will edit the question.

1 answer

4


To order you can use uasort in order to be able to navigate through the contents of each array with a callback, the first parameter will be the current item and the second will be the next one, then you make the comparison and return true or false.

To sort A-Z (alphabetical) it is necessary to use strcmp

Sort by the amount:

<?php
$teste = array(
    array(
        'id' => '107',
        'nome' => 'LAREIRA PEQ.',
        'qtd' => 43,
        'total' => 64500
    ),
    array(
        'id' => '108',
        'nome' => 'CHORAQUERIA PEQ.',
        'qtd' => 60,
        'total' => 48000
    ),
    array(
        'id' => '109',
        'nome' => 'JOGO DE FACAS',
        'qtd' => 90,
        'total' => 27000
    ),
    array(
        'id' => '111',
        'nome' => 'PROVALEIRA',
        'qtd' => 100,
        'total' => 6000
    ),
    array(
        'id' => '110',
        'nome' => 'COOLERS',
        'qtd' => 84,
        'total' => 21000
    ),
    array(
        'id' => '112',
        'nome' => 'CHAMPANHEIRA',
        'qtd' => 28,
        'total' => 1962.64
    )
);

uasort($teste, function ($a, $b) {
    return $a['qtd'] < $b['qtd'];
    //Se quiser inverter a ordem basta trocar por return $a['qtd'] > $b['qtd'];
});

print_r($teste);

Sort by name:

<?php
$teste = array(
    array(
        'id' => '107',
        'nome' => 'LAREIRA PEQ.',
        'qtd' => 43,
        'total' => 64500
    ),
    array(
        'id' => '108',
        'nome' => 'CHORAQUERIA PEQ.',
        'qtd' => 60,
        'total' => 48000
    ),
    array(
        'id' => '109',
        'nome' => 'JOGO DE FACAS',
        'qtd' => 90,
        'total' => 27000
    ),
    array(
        'id' => '111',
        'nome' => 'PROVALEIRA',
        'qtd' => 100,
        'total' => 6000
    ),
    array(
        'id' => '110',
        'nome' => 'COOLERS',
        'qtd' => 84,
        'total' => 21000
    ),
    array(
        'id' => '112',
        'nome' => 'CHAMPANHEIRA',
        'qtd' => 28,
        'total' => 1962.64
    )
);

uasort($teste, function ($a, $b) {
    return strcmp($a['nome'], $b['nome']);
    //Se quiser inverter a ordem basta trocar por return strcmp($b['nome'], $a['nome']);
});

print_r($teste);
  • 2

    Congratulations on the great response. Thank you very much.

  • @Hugoborges if you want decreasing change $a['qtd'] < $b['qtd'] for $a['qtd'] > $b['qtd'] and strcmp($a['nome'], $b['nome']) for strcmp($b['nome'], $a['nome'])

  • 1

    Blz I had already tested this hahahaha. Well I have one more debt, of how to write this array using the foreach, but this will be a new question

  • @Hugoborges sends the link I answer.

  • 1

    ok http://answall.com/questions/188479/como-dar-um-echo-no-array-usando-o-foreach

Browser other questions tagged

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