Greetings.

$result = array(); $i = 0; $find = $html->find('.class'); foreach($find as $value) { $result[$i]['example'] = $value->plaintext; $i++; } 

Is it possible to sort the $ result [$ i] ['seed'] array in descending order? Please suggest how to do this. Already tried all the functions of sorting, sorting somehow wrong.

Thank you in advance.

  • 3
    Well, I understand that this is [all the same topic] [1] that you left homeless. Let's not raise questions, but understand gradually. Show part of the original array. [1]: hashcode.ru/questions/254664/… - Deonis
  • The method that I used in the previous topic was sorted like this: There are numbers: 9, 927, 99, 97. In sorted form it will look like this: 99, 97, 927, 9. Required: 927, 99, 97, 9. Part of the source array through var_dump? - evansive
  • [0] => array (1) {["example"] => string (8) "0"} [1] => array (1) {["example"] => string (8) "0"} [2] => array (1) {["example"] => string (8) "0"} [3] => array (1) {["example"] => string (8) "0"} [4] => array (1) {["example"] => string (8) "0"} [5] => array (1) {["example"] => string (8) "0"} [6] => array (1) {["example"] => string (8) "0"} [7] => array (1) {["example"] => string (8) "0"} [8] => array (1) {["example"] => string (9) "16"} [9] => array (1) {["example"] => string (9) "11"} - evansive
  • @evansive, you are not confused by the fact that we visually observe three characters in the meaning, and var_dump gives us eight instead? Check the encoding - [mb_detect_encoding ()] [1] [1]: ua2.php.net/manual/ru/function.mb-detect-encoding.php - Deonis
  • Produces ASCII. - evansive

2 answers 2

In general, try to sort it out like this:

 function mnsort($a, $b){return strnatcmp($a['seed'],$b['seed']);} usort($arr, 'mnsort'); 

If you don’t succeed, you’ll have to search for invisible characters in the data, where they come from or what they are called. When you copy-paste the results here, these symbols disappear and it is not possible to reveal them from our side.

PS The first thing you notice: how this data is formed, where it comes from, what encoding. If you create them yourself, then I repeat - make sure that your page is utf-8 encoded without BOM. Especially if this data comes to you in response to an ajax request from some other page. Process the data for "control sequences":

 trim($text, " \t\n\r\0\x0B"); 
  • @Deonis, thank you so much, earned! $ by = 'seed'; usort ($ result, function ($ a, $ b) use ($ by) {return strnatcmp ($ a [$ by], $ b [$ by]);}); How to sort ascending? - evansive
  • one
    So swap the compared values. return strnatcmp ($ a [$ by], $ b [$ by]); // return return strnatcmp ($ b [$ by], $ a [$ by]); // Syud - Deonis
  • @Deonis, many thanks again! - evansive
  • and why is there a strnatcmp if you can just return (int) $ b ['seed'] - (int) $ a ['seed']; ? ideone.com/uNd66j - zb '
  • one
    @eicto, the type conversion variant, was [suggested earlier] [1], but, as I understood it, it did not roll ( why is another question ). [1]: hashcode.ru/questions/254664#254719 - Deonis

Here is another sorting function. Sort by two values.

  function mysortfunc($a, $b) { if ($a['itog']>$b['itog']) { return -1; } if ($a['itog']<$b['itog']) { return 1; } if ($a['itog']==$b['itog']) { if ($a['col']==$b['col']) return 0; return ($a['col']>$b['col']) ? -1 : 1; } } usort($mas, 'mysortfunc');