PHP - CSV到表格不按预期转换 [英] PHP - CSV to table not converting as expected

查看:102
本文介绍了PHP - CSV到表格不按预期转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从CSV文件创建SQL INSERT语句。我已经成功打开并读取文件。我甚至以表格格式输出文件。但是,我在for循环中做的更改,例如 $ c == 0 不起作用。它只是输出到表,就像它在csv文件中。这就是我想改变!为了保持这个例子,我试图将John Doe命名为john和Doe。 CSV文件的名称为一个,我想分成第一个和最后一个。

I am trying to create SQL INSERT statements from a CSV file. I have successfully opened and read the file. I have even output the file in a table format. However, the alterations I do in the for loop such as when $c == 0 do not work. It just outputs to the table exactly as it was in the csv file. That is what I am trying to change! To keep with that example, I am trying to make the name "John Doe" be "john" and "Doe". The CSV file has the names as one and I'd like to split into first and last.

此外,电话号码也不会改变。更改它们的代码以 $ c == 5 开头。有趣的是,当我把它们放在这里: http://ideone.com/HfGXJk 它工作正常。 / p>

Also, the phone numbers aren't changing either. The code to change them begins with $c == 5. The funny thing is when I put them into here: http://ideone.com/HfGXJk It works fine.

<?php

fgetcsv_PHP();

function fgetcsv_PHP()
{
if (($handle = fopen("guests.csv", "r")) !== FALSE)
{

    $length = 1000;
    $delimiter = ",";

    $fname = array();
    $lname = array();
    $address = array();
    $city = array();
    $state = array();
    $zip = array();
    $phone = array();
    $email = array();

    //print opening table
    echo "<table border='1'>\n";


    while ( ( $data = fgetcsv( $handle, $length, $delimiter ) ) !== FALSE )
    {
        // Count number of array elements in $data
        $num = count($data);

        // Print opening table row HTML tag
        echo "<tr>\n";

        //loop through array
        for ($c=0; $c < $num; $c++)
        {
            if ($c == 0)
            {
                $name = $c;
                $name = explode(" ",$name);
                $first = array_shift($name);
                $last = array_pop($name);
                array_push($fname, $first);
                array_push($lname, $last);
                echo "<td>".$data[$first]."</td>\n";
            }
            if ($c == 1)
            {
                array_push($address, $c);
                echo "<td>".$data[$c]."</td>\n";
            }
            if ($c == 2)
            {
                array_push($city, $c);
                echo "<td>".$data[$c]."</td>\n";
            }
            if ($c == 3)
            {
                array_push($state, $c);
                echo "<td>".$data[$c]."</td>\n";
            }
            if ($c == 4)
            {
                array_push($zip, $c);
                echo "<td>".$data[$c]."</td>\n";
            }
            if ($c ==5)
            {
                $phnum = $c;
                $phnum = preg_replace('~[^0-9]~','',$phnum);
                array_push($phone, $phnum);
                echo "<td>".$data[$phnum]."</td>\n";
            }
            if ($c == 6)
            {
                array_push($email, $c);
                echo "<td>".$data[$c]."</td>\n";
            }
        }

        // Print closing table row HTML tag
        echo "</tr>\n";
    }

    // Print close table HTML tag
    echo "</table>";

    // Close the file pointed to by $handle
    fclose($handle);
}
}
?>


推荐答案

读取名称的部分将$ name设置为0 ,在非现有空间处将其展开,将来自数组的第一个元素(来自explode)的0放在$ first中,并输出$ data [$ first],意味着$ data [0] - 原始值。

The part reading the name, sets $name to 0, explodes it at the non existing space, puts the 0 from the first element of the array (from the explode) in $first and outputs $data[$first] meaning $data[0] - the original value.

重构到PHP 5.5:

Refactored to PHP 5.5:

$file = new SplFileObject('guests.csv', 'r');
$file->setFlags(SplFileObject::READ_CSV);
$file->setCsvControl(',', '"');

$converter = function($traversable) {
  foreach ($traversable as $data) {
    list($first, $last) = explode(' ', $data[0]);
    $address = $data[1];
    $city = $data[2];
    $state = $data[3];
    $zip = $data[4];
    $phone = preg_replace('([^\d])', '', $data[5]);
    $email = $data[6];

    $result = array(
      'first' => $first,
      'last' => $last,
      'address' => $address,
      'city' => $city,
      'state' => $state,
      'zip' => $zip,
      'phone' => $phone,
      'email' => $email,
    );
    yield $result;
  }
};

foreach ($converter($file) as $data) {
  var_dump($data);
} 

这篇关于PHP - CSV到表格不按预期转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆