Php 内爆和分解关联数组

Php 内爆和分解关联数组,php,csv,explode,implode,Php,Csv,Explode,Implode,我目前正在做一个学校的项目,无法设法弄清楚如何正确地内爆和爆炸阵列。我的目标是能够添加新用户、删除用户和更新用户。为了实现这一点,我需要正确获取键值对 多谢各位 <?php class index { function __construct(){} 为什么不将csv文件的内容作为一个字符串,使用explode两次,一次使用explode(“,”,$CSVString),然后对每个索引调用另一个explode(使用explode(“=”,$array[$i]),如果需要,请在第

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

多谢各位

<?php
class index {

    function __construct(){}

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

<?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]
?>
这段非常紧凑的代码创建了三个数组,每个数组对应一个信息(mail、Fname、Lname),并直接选取必要的字符串,对其进行修剪,然后将值放入变量中。
如果您需要进一步的帮助或解释,请告诉我。

您也可以将每行分解一次

Email,First Name,Last Name

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

personFile.csv

<?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
?>
只有那一行

list($fname, $lname) = explode(' ', $N1,2);


有人能解释一下“2”在这里表示什么吗?

为什么不将csv文件的内容作为字符串,使用explode两次,一次使用explode(“,”,$CSVString),然后对每个索引调用另一个explode with explode(“=”,$array[$i]),如果需要,使用trim()在第二次爆炸前后,去掉结尾和开头的空格-如果这样做了,我会把它作为一个答案,如果不让我知道:)我做了以下操作,它说第二次爆炸应该是一个字符串。foreach($info形式的行){$CSVString=explode(',',$info);$last=explode('=',$CSVString);echo“”;print_r($last);echo“”;请参阅下面的我的答案。我希望它能帮助您:)看起来不错,因此,如果我想使用电子邮件作为密钥来取消某个特定用户的设置,我可以在数组中进行循环吗?如果电子邮件==$email unset myarray?我真的不知道您想要做什么…如果您最初的问题已经得到回答,我将非常感谢您的接受答案:)作为您的第二个问题问题,请进一步描述或创建一个新的。如果您在此处发布第二个链接,那么我也可以查看它。现在我需要在显示屏旁边放置一个编辑按钮。我成功地放置了一个Href edit,通过传递$email、$firstname和$lastname并通过编辑功能接收post请求。问题是编辑信息正在被删除添加到底部,我需要一种方法,用新的编辑值替换该值并重写数组test@mail.comtest\u firstname test\u lastname编辑test@mail.comtest\u firstname test\u lastname edit如果您有新问题,请提问。不要在评论中发布问题。否则,它已被标记并被删除。
    public function form(){
        include('add.html');
    }

} // close off class index

?>
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
?>
list($fname, $lname) = explode(' ', $N1,2);