【问题标题】:implode and explode associative array内爆和爆炸关联数组
【发布时间】:2016-02-08 15:13:55
【问题描述】:

我目前正在处理一个学校项目,但无法弄清楚如何正确地内爆和分解阵列。我的目标是能够添加新用户、删除用户和更新用户。为了实现这一点,我需要正确获取我的键值对。

谢谢

<?php
class index {

    function __construct(){}

我的CSV读取结果如下

大批 ( [电子邮件 = test@mail.com] => 名字 = fake_firstname )
    public function display() {

        $filename   = 'testfile.csv';
        $lines      = file($filename);
        $data       = array();

        echo "<tr>
        <th>Email</th>
        <th>First</th>
        <th>Last</th>
        </tr><br>";
        foreach($lines as $info) {
            list($key, $value) = explode(',', $info);
            $result[$key] = $value;

            echo "<pre>";
            print_r($result);
            echo "</pre>";
        }
    }

    public function add($Fname, $Lname, $Email) {
        $this->Fname = $Fname;
        $this->Lname = $Lname;
        $this->Email = $Email;
        $this->person = array('Email'=>$this->Email,'FirstName' =>$this->Fname,
        'LastName' =>$this->Lname);
        $this->save($this->person);

        print_r($this->person);
    }

    public function save($arr) {
        $filename = 'testfile.csv';
        $myfile = fopen($filename, "a+") or die("Unable to open file!");
        foreach($arr as $key => $value){
            $new[] = $key.' = '.$value;
            $final =  implode(",", $new);
        }
        fwrite($myfile, $final."\r\n");

        fclose($myfile);
    }

我的 CSV 保存结果如下Email = test@mail.com,FirstName = fake_firstname,LastName = fake_lastname

    public function form(){
        include('add.html');
    }

} // close off class index

?>

【问题讨论】:

  • 为什么不将 csv 文件的内容作为字符串获取,使用两次explode,一次使用explode(",", $CSVString),然后对于每个索引调用另一个explode 使用explode(" =", $array[$i]) 以及是否需要在第二次爆炸时使用 trim() 以消除末尾和开头的空格 - 如果这样做可以解决问题,我会将其作为答案,如果不让我知道:)
  • 我做了以下事情,它说第二个爆炸应该是一个字符串。 foreach($lines as $info) { $CSVString = explode(',', $info); $last = explode('=', $CSVString);回声“
    ”; print_r($last);回声“
    ”;
  • 见下面我的回答。希望对你有帮助:)

标签: php csv explode implode


【解决方案1】:

为什么不将 csv 文件的内容作为字符串获取,使用两次explode,一次使用explode(",", $CSVString),然后为每个索引调用另一个explode 使用explode("=", $array [$i]) 以及是否需要在第二次爆炸前后使用 trim() 以消除结尾和开头的空格。请注意,第二次爆炸是使用数组,因此您必须使用

$email = trim(explode("=", $yourArray[0])[1]);
$Fname = trim(explode("=", $yourArray[1])[1]);
$Lname = trim(explode("=", $yourArray[2])[1]);

这个非常紧凑的代码创建了三个数组,一个用于每个信息(邮件、Fname、Lname),并直接选择必要的字符串,修剪它并将值放入变量中。 如果您需要进一步的帮助或解释,请告诉我。

【讨论】:

  • 看起来不错,所以如果我想使用电子邮件作为键来取消设置特定用户,我可以通过数组进行循环,如果电子邮件 == $email unset myarray?
  • 我真的不明白您想要做什么...如果您的原始问题已得到回答,我将不胜感激您的接受回答 :) 对于您的第二个问题,请进一步描述或创建新问题一。如果您在此处发布第二个链接,那么我也可以查看它。
  • 现在我需要在显示器旁边放置一个编辑按钮。我设法通过 $email、$firstname 和 $lastname 进行 Href 编辑,并在编辑功能上接收发布请求。问题是编辑信息被添加到底部我需要一种方法用新的编辑值替换值并重写数组。电子邮件首先最后 test@mail.com test_firstname test_lastname 编辑 test@mail.com test_firstname test_lastname 编辑
【解决方案2】:

显然,您可以控制 .csv 文件的结构。所以我说,使用键作为 .csv 中的标题。 personFile.csv 应该只从其中的标题开始

personFile.csv

Email,First Name,Last Name

只有那一行

<?php
class index {

    function __construct(){}

    public function display() {
        $filename   = 'personFile.csv';
        $lines      = file($filename);
        $data       = array(); // store the contents of the file in an associative array for future use?

        // the first row is the header
        $headers = $lines[0];
        $countHeaders = count($headers); // store the count for future use
        echo "
        <tr>
            <th>" . implode("</th><th>",explode(",",$headers)) . "</th>
        </tr>";

        // array_slice($lines, 1) will return all the lines in $lines except for the first line
        foreach(array_slice($lines,1) as $row) {
            echo "
        <tr>
            <td>" . implode("</td><td>",explode(",",$row)) . "</td>
        </tr>";
        }
    }
    public function add($Fname, $Lname, $Email) {
        $this->Fname = $Fname;
        $this->Lname = $Lname;
        $this->Email = $Email;
        $this->person = array('Email'=>$this->Email,'FirstName' =>$this->Fname,
        'LastName' =>$this->Lname);
        $this->save($this->person);

        print_r($this->person);
    }

    public function save($arr) {
        $filename = 'personFile.csv';
        $myfile = fopen($filename, "a+") or die("Unable to open file!");

        $final = "\r\n" . implode(',',$arr);

        fwrite($myfile, $final);

        fclose($myfile);
    }
    public function form(){
        include('add.html');
    }
    //return true if the person was found and removed. else return false
    public function removePersonUsingEmail($emailToRemove) {
        $filename = 'personFile.csv';
        $lines      = file($filename);

        // the first row is the header
        $headers = $lines[0];
        $emailColumn = array_search("Email",$headers);

        $lines = array_slice($lines, 1);
        // array_slice($lines, 1) will return all the lines in $lines except for the first line
        foreach($lines as $rowNum => $row) {
            if($row[$emailColumn] === $emailToRemove) {
                unset($lines[$rowNum]);
                $final = implode(",",$headers);
                foreach($lines as $row) {
                    $final .= "\r\n" . implode(",",$row);
                }

                $myfile = fopen($filename, "w") or die("Unable to open file!");
                fwrite($myfile, $final);
                fclose($myfile);
                return true;
            }
        }

        return false;
    }
} // close off class index
?>

【讨论】:

    【解决方案3】:

    你也可以每行爆炸一次

    <?php
    
    $line = 'Email = test@mail.com,FirstName = fake_firstname,LastName = fake_lastname';
    
    $parts = explode( ',', str_replace( array( ' = ', 'Email', 'FirstName', 'LastName'), '', $line ) );
    
    // email = $parts[0]
    // firstname = $parts[1]
    // lastname = $parts[2]
    ?>
    

    【讨论】:

      【解决方案4】:
      list($fname, $lname) = explode(' ', $N1,2);
      

      谁能解释一下这里的“2”是什么意思?

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-23
      • 2019-07-26
      • 1970-01-01
      • 2020-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-18
      相关资源
      最近更新 更多