sorting files with numbers after dots - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.

Name: sorting files with numbers after dots fullscreencopydownloadembedprint


Your result can be seen below.

Result of php executing





Full code of sorting files with numbers after dots.php

  1. <?php
  2.  
  3.  
  4. $array_of_files = [
  5. "Program2/2021.03.21/INV20210310.33.7z",
  6. "Program2/2021.03.22/INV20210310.32.7z",
  7. "Program2/2021.03.21/INV20210310.22.7z",
  8. "Program2/2021.03.20/INV20210311.7.7z",
  9. "Program1/2021.03.21/INV20210310.1.7z",
  10. "Program1/2021.03.21/INV20210310.72.7z",
  11. "Program1/2021.03.21/INV20210310.22.7z",
  12. "Program1/2021.03.21/INV20210311.7.7z"
  13. ];
  14.  
  15. function split_array_of_strings_to_array(array $input) :array {
  16.     $output = [];
  17.     $arr_elements = count($input);
  18.     for($x=0; $x<$arr_elements; $x++) {
  19.         $output []= split_string_to_array($input[$x]);
  20.     }
  21.     return $output;
  22. }
  23.  
  24. function split_string_to_array(string $input) :array {
  25.     $output = [];
  26.     $explode_1 = explode("/", $input);
  27. for($x=0; $x<count($explode_1); $x++) {
  28.    if($x!=0) {
  29.        $output []= "+";
  30.    }
  31. if(stripos($explode_1[$x], ".") !== -1) {
  32.    $explode_2 = explode(".", $explode_1[$x]);
  33.    for($y=0; $y<count($explode_2); $y++) {
  34.        if($y!=0) {
  35.           $output []= "&";
  36.        }
  37.        $output []= $explode_2[$y];
  38.    }
  39. }
  40. else {
  41.    $output []= $explode_1[$x];
  42. }
  43. }
  44. return $output;
  45. }
  46.  
  47. function compare_on_global_index($a,$b) {
  48.     global $global_index;
  49.     return ($a[$global_index] <= $b[$global_index]) ? -1 : 1;
  50. }
  51.  
  52.  
  53. function get_highest_number_of_elements_in_subarray(array $input):int {
  54.     $highest = 0;
  55.     for($x=0; $x<count($input); $x++) {
  56.         if(count($input[$x])>$highest) {
  57.             $highest = count($input[$x]);
  58.         }
  59.     }
  60.     return $highest;
  61. }
  62.  
  63. function implode_back_array_of_files(array $input_array):array {
  64.     for($x=0; $x<count($input_array); $x++) {
  65.         $input_array[$x] = implode($input_array[$x]);
  66.         $input_array[$x] = str_replace(["&", "+"], [".", "/"], $input_array[$x]);
  67.     }
  68.     return $input_array;
  69. }
  70.  
  71.  
  72.  
  73. $splitted_arr = split_array_of_strings_to_array($array_of_files);
  74.  
  75. for($x=10; $x>-1; $x-=2) {
  76.     $global_index=$x;
  77.     usort($splitted_arr, "compare_on_global_index");
  78. }
  79.  
  80. print_r($splitted_arr);
  81.  
  82. print_r(implode_back_array_of_files($splitted_arr));
File Description
  • sorting files with numbers after dots
  • PHP Code
  • 23 Mar-2021
  • 2.03 Kb
You can Share it: