Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

127
Visualizações
How to sort an array within an associative array with its value using php

I have an array like:

$a = array(array("fghfg12" => 34),
           array("dfgdf23" => 97),
           array("wetw13" => 65),
           array("rtyr567" => 18));

I want to sort this array by its value. That means I want the result like:

$a = array(array("rtyr567" => 18),
           array("fghfg12" => 34),
           array("wetw13" => 65),
           array("dfgdf23" => 97));

For this I am using:

uksort($a , function($key1, $key2) use ($res) {
      return (array_search($key1, $res) > array_search($key2, $res));
  });

Another method:

$arr2ordered = array() ;
foreach (array_keys($a) as $key) {
   $arr2ordered[$key] = $a[$key] ;
}

But I didn't get my result;

I already tried with this also:

$price = array();
    foreach ($a as $key => $row)
    {
        $price[$key] = $row;
    }
    array_multisort($price, SORT_DESC, $a);
    print_r($a);

But still, I didnt get my result

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Since you don't know what the key of each inner array will be, you can map current() over $a to get the first value in each inner array, then use that array of values as the first argument to array_multisort() to sort $a.

array_multisort(array_map('current', $a), $a);

Check it out: https://3v4l.org/4Zu0q

over 4 years ago · Santiago Trujillo Relatório

0

try that code

$a = array(array("fghfg12" => 34),
       array("dfgdf23" => 97),
       array("wetw13" => 65),
       array("rtyr567" => 18));

usort($a, function($a, $b){
return array_values($a) > array_values($b);
});
       

But only for your structure..

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda