Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- input.csv:
- ENTITY;id1;id2;id3
- Ajka;a;1;x
- Baja;b;2;
- Cikó;b;3;
- Dány;;3;
- Ebes;d;;y
- Output:
- Ajka;1
- Baja;2
- Cikó;2
- Dány;2
- Ebes;3
- <?php
- $f_=file('input.csv',FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
- define('d',";");
- define('n',"\n");
- $li=$entity=$element2list=array(); $lun=0;
- foreach($f_ as $i=>$f){
- if ($i==0) continue; //header
- $x=explode(d,$f);
- if ($i==1) $siz=sizeof($x);
- $li[$f]=1;
- $entity[$x[0]][]=$x;
- }
- foreach($li as $t=>$dummy){
- $xx=explode(d,$t);
- $foundx=false;
- foreach($xx as $x){ if (empty($x)) continue; if (!empty($element2list[$x])) $foundx=$element2list[$x]; }
- if (!$foundx) {
- $lun++; foreach($xx as $x){ if (!empty($x)) $element2list[$x]=$lun; } } else {
- foreach($xx as $x){ if (!empty($x)) $element2list[$x]=$foundx; } } }
- foreach($entity as $m=>$t){
- foreach($t as $j=>$v){
- for($i=0;$i<$siz;$i++){
- if (empty($v[$i])) continue;
- print $m.d. /*$j.d.$v[$i].d.*/ $element2list[$v[$i]].n;
- break;
- }}}
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement