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.
Result of php executing
Full code of sorting files with numbers after dots.php
- <?php
- $array_of_files = [
- "Program2/2021.03.21/INV20210310.33.7z",
- "Program2/2021.03.22/INV20210310.32.7z",
- "Program2/2021.03.21/INV20210310.22.7z",
- "Program2/2021.03.20/INV20210311.7.7z",
- "Program1/2021.03.21/INV20210310.1.7z",
- "Program1/2021.03.21/INV20210310.72.7z",
- "Program1/2021.03.21/INV20210310.22.7z",
- "Program1/2021.03.21/INV20210311.7.7z"
- ];
- function split_array_of_strings_to_array(array $input) :array {
- $output = [];
- $arr_elements = count($input);
- for($x=0; $x<$arr_elements; $x++) {
- $output []= split_string_to_array($input[$x]);
- }
- return $output;
- }
- function split_string_to_array(string $input) :array {
- $output = [];
- $explode_1 = explode("/", $input);
- for($x=0; $x<count($explode_1); $x++) {
- if($x!=0) {
- $output []= "+";
- }
- if(stripos($explode_1[$x], ".") !== -1) {
- $explode_2 = explode(".", $explode_1[$x]);
- for($y=0; $y<count($explode_2); $y++) {
- if($y!=0) {
- $output []= "&";
- }
- $output []= $explode_2[$y];
- }
- }
- else {
- $output []= $explode_1[$x];
- }
- }
- return $output;
- }
- function compare_on_global_index($a,$b) {
- global $global_index;
- return ($a[$global_index] <= $b[$global_index]) ? -1 : 1;
- }
- function get_highest_number_of_elements_in_subarray(array $input):int {
- $highest = 0;
- for($x=0; $x<count($input); $x++) {
- if(count($input[$x])>$highest) {
- $highest = count($input[$x]);
- }
- }
- return $highest;
- }
- function implode_back_array_of_files(array $input_array):array {
- for($x=0; $x<count($input_array); $x++) {
- $input_array[$x] = implode($input_array[$x]);
- $input_array[$x] = str_replace(["&", "+"], [".", "/"], $input_array[$x]);
- }
- return $input_array;
- }
- $splitted_arr = split_array_of_strings_to_array($array_of_files);
- for($x=10; $x>-1; $x-=2) {
- $global_index=$x;
- usort($splitted_arr, "compare_on_global_index");
- }
- print_r($splitted_arr);
- print_r(implode_back_array_of_files($splitted_arr));