Search code examples
phplisttextmultiple-columns

Print the elements of a txt column


Got this code php

$data ='
one;uno
two;dos
three;tres
four;cuatro
'

I want to print the first column in a row, separating elements with comma and aspace, to obtain this result:

one, two, three, four

Any help please? I'm doing this but I can`t:

<?php

$data ='
one;uno
two;dos
three;tres
four;cuatro
';

$line = explode("\n", $data);
for($i = 0; $i<count($line); $i++) {        

$item = explode(";", $line[$i]);

$coma = implode(', ', $item[0{);

echo $coma;

}
?>

Solution

  • try this modified version of your code

    $data ='one;uno
    two;dos
    three;tres
    four;cuatro';
    $coma=array();
    $line = explode("\n", $data);
    
    for($i = 0; $i<count($line); $i++) {        
    $item = explode(";", $line[$i]);
    $coma[]=  $item[0];
    }
    
    echo implode(',',$coma);