内爆和爆炸关联数组

implode and explode associative array

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

谢谢

<?php
class index {

    function __construct(){}

我的CSV读取结果如下

Array
(
    [Email = test@mail.com] => FirstName = 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("=", $array[$i]) 并且如果需要 trim() 围绕第二次爆炸以消除结尾和开头的空格。请注意,第二个爆炸是用一个数组,所以你必须使用

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

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

你也可以每行展开一次

<?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]
?>

很明显,您可以控制 .csv 文件的结构。所以我说,在 .csv 中使用键作为 headers。 personFile.csv 应该只以 headers 开头

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

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