简体   繁体   English

在fputcsv上的preg_split将元素数组存储到csv的不同列中

[英]preg_split on fputcsv to store a array of elements into different columns of csv

I'm trying to store the values of the table in to a csv file for which I used fputcsv function. 我正在尝试将表的值存储到使用fputcsv函数的csv文件中。 However it puts the elements into a single column. 但是,它将元素放在单个列中。 I need to store the values in three columns. 我需要将值存储在三列中。

Code: 码:

foreach ($total as $key => $value) 
{
        foreach ($final as $key1 => $value1)
        {
            if($value == $value1)
            {
            $total[$key] = null;
            break;
            }
        }
}
foreach($total as $key =>$value)
{   
    if(($total[$key] != null) && (!preg_match("/^bb_demo_/",$total[$key])))
    {
    array_push($check, $value,$ext1[$key], $ext2[$key]);
    echo "<table border='1' width='400px' cellpadding='1' cellspacing='1'>";
    echo "<tr><td width='100px' align='center'><strong>$value</strong></td>";
    echo "<td width='100px' align='center'><b>";
    echo $key+1;
    echo "</td>";
    echo "<td width='100px' align='center'>";
    echo $ext1[$key];
    echo "</td>";
    echo "<td width='100px' align='center'>";
    echo $ext2[$key];
    echo "</td>";       
    }
}

I tried to get values from a csv file perfom certain operations on it and displayed them as a table in html. 我试图从csv文件获取对某些操作的值,并将其显示为html表。 Now I want these values stored in html table to be stored in a csv file So I created a new array $check and used the following code: 现在,我希望将存储在html表中的这些值存储在一个csv文件中,因此我创建了一个新数组$ check并使用了以下代码:

$file = fopen("contacts.csv","w");
foreach($check as $values)
{
    fputcsv($file,preg_split('//',$values));
}

fclose($file);

which stores values into one column 将值存储到一列中

Im thinking whether is there a way that I separate the first three elements separated by commas using 'preg_split' ie using preg_split('/,,,/)? 我在想是否有办法使用'preg_split'即用preg_split('/ ,,, /)分隔以逗号分隔的前三个元素? I know the above expression is incorrect But I don't have a clue of doing it? 我知道上述表达不正确,但是我不知道这样做吗? Could someone help me with this? 有人可以帮我吗? Thanks in advance 提前致谢

I considered three different loops to store the values of $value, e used a for loop as follows 我考虑了三个不同的循环来存储$ value的值,e使用了如下的for循环

Code for($i=0; $i < sizeof($check); $i++) { 代码for($i=0; $i < sizeof($check); $i++) {

fputcsv($file, array($check[$i], $check1[$i], $check2[$i])) ; }
fclose($file) ;

It works! 有用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM