trening - 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.



Your result can be seen below.

Result of php executing





Full code of trening.php

  1. <?php
  2. $input_arr = [
  3.         "Program2/2021.03.21/INV20210310.1.7z",
  4.         "Program2/2021.03.22/INV20210310.32.7z",
  5.         "Program2/2021.03.21/INV20210310.22.7z",
  6.         "Program2/2021.03.20/INV20210311.7.7z",
  7.         "Program1/2021.03.21/INV20210310.1.7z",
  8.         "Program1/2021.03.21/INV20210310.72.7z",
  9.         "Program1/2021.03.21/INV20210310.22.7z",
  10.         "Program1/2021.03.21/INV20210311.7.7z"
  11. ];
  12.  
  13.  
  14.  
  15.  
  16. function arrange_array(array $input_arr = []) :array {
  17.     
  18.         $assoc_array = [];
  19.     $iterations = count($input_arr);
  20.     for($x=0; $x<$iterations; $x++) {
  21.            
  22.             $row = explode_string_by_slash_to_array($input_arr[$x]);
  23.            
  24.                 $assoc_array[$row[0]]                   = empty($assoc_array[$row[0]]) ? [] : $assoc_array[$row[0]];
  25.                 $assoc_array[$row[0]][$row[1]]  = empty($assoc_array[$row[0]][$row[1]]) ? [] : $assoc_array[$row[0]][$row[1]];
  26.            
  27.             $separate_comma = explode_string_by_dot_to_array($row[2]);
  28.            
  29.             if(!isset($assoc_array[$row[0]][$row[1]][$separate_comma[0]])) {
  30.                 $assoc_array[$row[0]][$row[1]][$separate_comma[0]] = [];
  31.             }
  32.             $assoc_array[$row[0]][$row[1]][$separate_comma[0]] []= (int)$separate_comma[1];
  33.            
  34.     }
  35.        
  36.         foreach($assoc_array AS $program => $program_name) {
  37.                 foreach($program_name AS $date => $filename) {
  38.                         foreach($filename AS $key => $value) {
  39.                                 sort($filename[$key]);
  40.                                 $filename[$key]=$value;
  41.                         }
  42.                        
  43.                         ksort($filename);
  44.                 }
  45.                 ksort($program_name);
  46.                 //print_r($value);
  47.                 //              sort($value);//sort by last number
  48.                 //      ksort($value);//sort by file (before comma)
  49.                 //ksort($assoc_array[$program]);//sort date
  50.         }
  51.         //ksort($assoc_array);
  52.     
  53.     
  54.     return $assoc_array;
  55. }
  56.  
  57. function recurrent_sort(array $input_array) :array {
  58.     foreach($input_array AS $key => $value) {
  59.         if(is_array($value)) {
  60.             $value = recurrent_sort($value);
  61.         }
  62.         sort($input_array);
  63.     }
  64.     return $input_array;
  65. }
  66.  
  67. function explode_string_by_dot_to_array(string $input): array {
  68.         return explode(".", $input);
  69. }
  70. function explode_string_by_slash_to_array(string $input): array {
  71.         return explode("/", $input);
  72. }
  73. function create_array_element_if_not_exist($element = NULL) {
  74.         if($element === NULL) {
  75.                 return [];
  76.         }
  77.         return $element;
  78. }
  79.  
  80.  
  81. //arrange_array($input_arr);
  82. print_r(arrange_array($input_arr));
  83. //print_r(arrange_array($input_arr))
  84.  
  85.  
File Description
  • trening
  • PHP Code
  • 22 Mar-2021
  • 2.28 Kb
You can Share it: