Sort foreach in php

Asked

Viewed 771 times

1

I made a page that shows the hash in several hash_algos in order to study their differences, but I would like to sort by the hash size and not by the hash_algos name, the code is this,

<?php foreach (hash_algos() as $hash_algos) {
        $hash = hash($hash_algos, "salt", false); ?>
        <li>
            <span><?php echo $hash_algos; ?></span>
            <span><?php echo strlen($hash); ?></span>
            <span><?php echo $hash; ?></span>
        </li>
<?php } ?>

would be like when printing, be ordered by the size of the hash that in the case depending on the algos it will have from 8 until 128 and make a sub Sort with the algos, ie put all of 8 with all fnv, the second Sort is not so important, I just really wanted to see how to sort out what size indicates.

example in ideone

Grateful from now on

2 answers

2


It would be the case to first collect the data, and order while displaying:

   // Primeiro coletamos os resultados em $results

   foreach (hash_algos() as $hash_algos) {
      $hash = hash($hash_algos, "salt", false);
      $results[] = ['algo' => $hash_algos, 'len' => strlen($hash), 'hash' => $hash];
   }

   // Em seguida usamos usort($array, 'funcao') para ordenar com uma função própria

   function compara($a, $b) {
      return $a['len'] - $b['len'];
   }
   usort($results, 'compara');

   // finalmente exibimos o resultado final, já ordenado

   foreach ($results as $result) {
      echo $result['algo'].' '.$result['len'].' '.$result['hash'].PHP_EOL;
   }

See working on IDEONE.


Better understand the usort() in the PHP manual:

http://php.net/manual/en/function.usort.php

  • perfect, very cool, I saw in many places I needed to create another Function but I was not understanding how to do, this PHP_EOL at the end serves for what same?

  • correct if you are wrong, it is to make the line break?

  • @flourigh is just to break the lines in the demo (equivalent to " n" or " r n" of the operating system, in your case just hit the HTML). The important thing is to understand the function usort

  • yes, it inside the function will see who is bigger and put it in the queue up or down to compare with the next, I had no interaction with it even before you show me, very good.

1

You can also use the arsort:

$algos = array_flip(hash_algos());

foreach($algos as $algo => $len){
      $algos[$algo] = strlen(hash($algo, '', true));
}

arsort($algos);

foreach($algos as $algo => $len){
    echo $algo . ' => ' .$len;
    echo '<br>';
}

Remembering that using the false in the last parameter of hash, as used in your question, will return the hash size in HEX. The hexadecimal is that twice the original length.

A SHA-512 has exact 512 bits, but encoded for hexadecimal will use 1024 bits to store 512 bits. Already using Base64 the same 512 bits would pass to 704 bits, for comparison. So I believe what you want is the original length and not hexadecimal, even because you may not use hexadecimal.

  • um, really good, I’ll compare the two, but I really liked yours too

Browser other questions tagged

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