数据块到数据行

时间:2014-04-22 13:17:54

标签: php arrays parsing csv

我有一个包含解析为CSV的文本数据块的数组:

"Cecilia Elisabeth Arvidsson"
"Kungsvägen 118 A 35244 VÄXJÖ"
"Skolgatan 1 B 35233 VÄXJÖ"
1969-06-17
0735372562,
"Pär Davidsson"
"Skolgatan 1 B 35233 VÄXJÖ"
"Myntvägen 8 35260 VÄXJÖ"
1980-05-22
0709171505,

我想将这些块转换为简单的文本行,因此可以轻松导入到excell:

"Cecilia Elisabeth Arvidsson Kungsvägen 118 A 35244 VÄXJÖ Skolgatan 1 B 35233 VÄXJÖ 1969-06-17 0735372562",
"Pär Davidsson Skolgatan 1 B 35233 VÄXJÖ Myntvägen 8 35260 VÄXJÖ 1980-05-22 0709171505",

我该如何做到这一点?

这是我的PHP代码:

    if(isset($_POST['submit']))
{
    $gatunamn = $_POST['gata'];
    $ort = $_POST['omrade'];
    $csv_data = array();



    $newSpider->fetchPage($gatunamn, $ort, $offset=0);
    $obj = json_decode($newSpider->html);


    echo "<div id='rightcontent'><table id='one-column-emphasis'>";
    echo "<th><input type='checkbox' name='csv_all' id='csv_all'></th><th>Namn</th><th>Adress</th><th>Adress2</th><th>Adress3</th><th>Personnummer</th><th>Telefonnummer</th><th>Telefonnummer2</th>";
    $antal_sidor = round($obj->search->wp->totalHits / $obj->search->wp->pageSize); 
    echo "<td></td>";
    foreach($obj->search->wp->features as $fish) //Loopar ut 50st (pageSize)
    {
        echo "<tr>";
        echo "<td><input type='checkbox' value='csv' class='csv'></td>";
        echo "<td>" . $fish->name . "</td>";
        $csv_data[] .= $fish->name;

        foreach($fish->addresses as $ad)
        {
            echo "<td>" . $ad->label . " " . $ad->postcode . " " . $ad->area . "</td>";
            $csv_data[] .= $ad->label . " " . $ad->postcode . " " . $ad->area;
        }   
        if(!empty($fish->dateOfBirth))
        {
            $convert_date = substr($fish->dateOfBirth, 0, -3); //Gör om datum från timestamp
            echo "<td>" . date("Y-m-d", $convert_date) . "</td>";
            $convert_datee = date("Y-m-d", $convert_date);
            $csv_data[] .= $convert_datee;


        }
        if(!empty($fish->phoneNumbers))
        {
            foreach($fish->phoneNumbers as $ph)
            {
                echo "<td>" . $ph . "</td>";
                $csv_data[] .= $ph . ",";

            }
        }       

        echo "</tr>";

    }
    echo "</table>";
    $j = 0;
    for($i = 1; $i <= $antal_sidor; $i++)
    {
        echo  "<a href='curl.php?gatunamn=$gatunamn&ort=$ort&offset=$j'>" . $i . "</a> ";
        $j += 100;
    }
    echo "</div>";
    /*echo "<div id='debug'><pre>"; 
        var_dump($obj);
    echo "</pre></div>";*/

    }
    if(isset($_POST['export']))
    {

        $fp = fopen("eniro.csv","w");
        fputcsv($fp,$csv_data,"\n");

        echo "<div id='csv_info'>";
        echo "<a href='eniro.csv'>Hämta CSV-fil</a>";
        echo "</div>";
    }

}

编辑:我用相关代码更新了我的问题。

2 个答案:

答案 0 :(得分:0)

$vStringlist = implode(' ', $csv_data)

这样你就会得到一个数组,将每个块作为单行包含在一起,用空格连接。

答案 1 :(得分:0)

我仍然不完全确定你想要存档什么,但我写了这个小脚本来完成你在你的例子中命名的内容:

// Lets say you recive your data as a string:
$blocks = '
"Cecilia Elisabeth Arvidsson"
"Kungsvägen 118 A 35244 VÄXJÖ"
"Skolgatan 1 B 35233 VÄXJÖ"
1969-06-17
0735372562,
"Pär Davidsson"
"Skolgatan 1 B 35233 VÄXJÖ"
"Myntvägen 8 35260 VÄXJÖ"
1980-05-22
0709171505,';

// Then we can simply explode your data by the comma separator
$blocks = explode( ",", $blocks );

// loop trough the items and format the text blocks.
foreach( $blocks as $key => $block )
{
    $blocks[$key] = trim( str_replace( "\n", ' ', str_replace( '"', '', $block ) ) );
}

$blocks = array_filter( $blocks );

// Will result:
// Array
// (
//    [0] => Cecilia Elisabeth Arvidsson Kungsvägen 118 A 35244 VÄXJÖ Skolgatan 1 B 35233 VÄXJÖ 1969-06-17 0735372562
//    [1] => Pär Davidsson Skolgatan 1 B 35233 VÄXJÖ Myntvägen 8 35260 VÄXJÖ 1980-05-22 0709171505
// )

// At the end you can implode your data again to match your example.
echo '"'.implode( '",'."\n".'"', $blocks ).'"'."\n";

// Will result:
// "Cecilia Elisabeth Arvidsson Kungsvägen 118 A 35244 VÄXJÖ Skolgatan 1 B 35233 VÄXJÖ 1969-06-17 0735372562",
// "Pär Davidsson Skolgatan 1 B 35233 VÄXJÖ Myntvägen 8 35260 VÄXJÖ 1980-05-22 0709171505"

更新

// with the following data you deliverd: http://pastebin.com/ThAbhpkZ using here as $data

$array_of_blocks = explode( ',', implode( ' ', $data ) );

// Will result:
// Array
// (
//    [0] => Cecilia Elisabeth Arvidsson Kungsvägen 118 A 35244 VÄXJÖ Skolgatan 1 B 35233 VÄXJÖ 1969-06-17 0735372562
//    [1] => Pär Davidsson Skolgatan 1 B 35233 VÄXJÖ Myntvägen 8 35260 VÄXJÖ 1980-05-22 0709171505
// )


// At the end you can implode your data again to match your example.
echo '"'.implode( '",'."\n".'"', $blocks ).'"'."\n";

// Will result:
// "Cecilia Elisabeth Arvidsson Kungsvägen 118 A 35244 VÄXJÖ Skolgatan 1 B 35233 VÄXJÖ 1969-06-17 0735372562",
// "Pär Davidsson Skolgatan 1 B 35233 VÄXJÖ Myntvägen 8 35260 VÄXJÖ 1980-05-22 0709171505"