1
resposta

uksort não funcionou

Eu fiz o seguinte código:

$notas = [

        ['nome'=>'Matheus', 'nota'=>10],

        ['nome'=>'Katniss', 'nota'=>11],

        ['nome'=>'Jao', 'nota'=>3],

        ['nome'=>'Marley', 'nota'=>6]

    ];



function ordenaArray(array $nota1, array $nota2): int
{
    return $nota2 <=> $nota1;
}

uksort($notas, 'ordenaArray');
var_dump($notas);

No entanto, ele não funcionou e gerou o seguinte erro:


C:\Users\mathe\Downloads\teste>php ordem.php
PHP Fatal error:  Uncaught ArgumentCountError: uksort() expects exactly 2 arguments, 1 given in C:\Users\mathe\Downloads\teste\ordem.php:44
Stack trace:
#0 C:\Users\mathe\Downloads\teste\ordem.php(44): uksort(Array)
#1 {main}
  thrown in C:\Users\mathe\Downloads\teste\ordem.php on line 44

Fatal error: Uncaught ArgumentCountError: uksort() expects exactly 2 arguments, 1 given in C:\Users\mathe\Downloads\teste\ordem.php:44
Stack trace:
#0 C:\Users\mathe\Downloads\teste\ordem.php(44): uksort(Array)
#1 {main}
  thrown in C:\Users\mathe\Downloads\teste\ordem.php on line 44

C:\Users\mathe\Downloads\teste>

Alguém poderia, por gentileza, me informar qual foi meu erro?

Desde já, agradeço

1 resposta

você estava utilizando o '<=>'para comprar dois arrays

function ordenaArray(array $nota1, array $nota2): int
{
    return $nota2['nota'] <=> $nota['nota']1;
}

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software