Advertisement
szabozoltan69

varosok-csoportok

Jan 15th, 2017
236
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. input.csv:
  2. ENTITY;id1;id2;id3
  3. Ajka;a;1;x
  4. Baja;b;2;
  5. Cikó;b;3;
  6. Dány;;3;
  7. Ebes;d;;y
  8.  
  9. Output:
  10. Ajka;1
  11. Baja;2
  12. Cikó;2
  13. Dány;2
  14. Ebes;3
  15.  
  16.  
  17. <?php
  18. $f_=file('input.csv',FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
  19. define('d',";");
  20. define('n',"\n");
  21.  
  22. $li=$entity=$element2list=array(); $lun=0;
  23.  
  24. foreach($f_ as $i=>$f){
  25.     if ($i==0) continue; //header
  26.     $x=explode(d,$f);
  27.     if ($i==1) $siz=sizeof($x);
  28.     $li[$f]=1;
  29.     $entity[$x[0]][]=$x;
  30. }
  31.  
  32. foreach($li as $t=>$dummy){
  33.     $xx=explode(d,$t);
  34.     $foundx=false;
  35.     foreach($xx as $x){ if (empty($x)) continue; if (!empty($element2list[$x])) $foundx=$element2list[$x]; }
  36.     if (!$foundx) {
  37.         $lun++; foreach($xx as $x){ if (!empty($x)) $element2list[$x]=$lun; } } else {
  38.                 foreach($xx as $x){ if (!empty($x)) $element2list[$x]=$foundx; } } }
  39.  
  40. foreach($entity as $m=>$t){
  41. foreach($t as $j=>$v){
  42. for($i=0;$i<$siz;$i++){
  43.     if (empty($v[$i])) continue;
  44.     print $m.d. /*$j.d.$v[$i].d.*/ $element2list[$v[$i]].n;
  45.     break;
  46. }}}
  47. ?>
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement