内爆和爆炸关联数组

时间:2015-11-07 15:17:59

标签: php csv explode implode

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

谢谢

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

?>

4 个答案:

答案 0 :(得分:1)

为什么不将csv文件的内容作为字符串获取,使用explode两次,一次使用explode(“,”,$ CSVString),然后为每个索引调用另一个使用explode爆炸(“=”,$ array [$ i])如果需要修剪()围绕第二次爆炸以摆脱结束和开始的空间。请注意,第二次爆炸是使用数组,因此您必须使用

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

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

答案 1 :(得分:1)

显然,您可以控制.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
?>

答案 2 :(得分:0)

你也可以爆炸每一行

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

答案 3 :(得分:0)

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

任何人都可以解释&#34; 2&#34;在这表示?

相关问题