2015-11-07 137 views
2

我目前正在研究一個學校項目,無法設法弄清楚如何正確爆炸和爆炸陣列。我的目標是能夠添加新用戶,能夠刪除用戶並更新用戶。爲了達到這個目標,我需要正確獲取我的關鍵值對。內爆和爆炸聯想陣列

謝謝

<?php 
class index { 

    function __construct(){} 

我的CSV讀取結果在下面的

 
Array 
(
    [Email = [email protected]] => 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 = [email protected],FirstName = fake_firstname,LastName = fake_lastname

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

} // close off class index 

?> 
+0

爲什麼不把csv文件的內容當作一個字符串來使用,使用explode兩次,一次使用explode(「,」,$ CSVString),然後爲每個索引調用另一個使用explode(「=」,$ array [$ i]),如果需要修剪()圍繞第二個爆炸來擺脫末尾和開始處的空間 - 如果這樣做的伎倆,我會把它作爲答案,如果不讓我知道:) – JRsz

+0

我做了以下,它說第二次爆炸應該是一個字符串。 foreach($ lines as $ info){ $ CSVString = explode(',',$ info); $ last = explode('=',$ CSVString); echo「

"; print_r($last); echo "
」; – Royce

+0

請參閱下面的答案。我希望它可以幫助你:) – JRsz

回答

1

你爲什麼不拿到csv文件的內容爲一個字符串,使用爆炸兩次,一次爆炸(「,」,$ CSVString),然後爲每個索引調用爆炸(「=」,$ array [$ i]),如果需要trim()第二次爆炸是爲了擺脫末端和開始的空間。需要注意的是第二個爆炸是一個數組,所以你必須使用

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

這種非常緊湊的代碼創建了三個陣列,每個信息(郵件,FNAME,LNAME),並直接挑選neccessary字符串,修剪它並將該值放入一個變量中。 如果您需要進一步的幫助或完善,請讓我知道。

+0

看起來不錯,所以如果我想解除一個特定的用戶使用電子郵件作爲關鍵,我可以做一個循環通過數組,如果電子郵件== $電子郵件未設置myarray? – Royce

+0

我真的沒有得到你想要做的事......如果你的原始問題已經答案,我將不勝感激從你接受的答案:)對於第二個問題,請進一步描述它或創建一個新的問題。如果你在第二篇文章中發佈鏈接,那麼我也可以檢查它。 – JRsz

+0

現在我需要在顯示屏旁邊放置一個編輯按鈕。我設法把一個HREF編輯傳遞給$ email,$ firstname和$ lastname並在編輯函數上接收發布請求。問題是編輯信息被添加到底部,我需要一種方法來用新的編輯值替換值並重寫數組。 電子郵件\t首先\t最後 [email protected] \t test_firstname \t test_lastname編輯 [email protected] test_firstname test_lastname編輯 – Royce

0

你也可能會引起爆炸的每一行一次

<?php 

$line = 'Email = [email protected],FirstName = fake_firstname,LastName = fake_lastname'; 

$parts = explode(',', str_replace(array(' = ', 'Email', 'FirstName', 'LastName'), '', $line)); 

// email = $parts[0] 
// firstname = $parts[1] 
// lastname = $parts[2] 
?> 
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 
?> 
只有線
0
list($fname, $lname) = explode(' ', $N1,2); 

誰能解釋一下什麼是 「2」在這裏表示?

+1

如果您有新的問題,請向它http://stackoverflow.com/questions/ask。不要在評論中發佈問題。其他方面,它已被標記並被刪除。 – ketan