在 PHP 中向檔案寫入陣列
- 
          
            使用 print_r()和file_put_contents()函式將陣列寫入 PHP 檔案
- 
          
            使用 fopen()、print_r()和fwrite()函式將陣列寫入 PHP 檔案
- 
          
            使用 fopen(),var_export()和fwrite()函式將陣列寫入檔案
 
我們將介紹一種使用 print_r() 和 file_put_contents() 函式將陣列寫入 PHP 檔案的方法。此方法將指定的陣列寫入系統中的指定檔案。
我們還將介紹一種使用 print_r() 和 fwrite() 函式將陣列列印到 PHP 檔案的方法。我們使用 fopen() 函式建立檔案。
我們將引入永久性解決方案來顯示所有 PHP 錯誤,並更改 php.ini 檔案。上面提到的兩種方法將無助於顯示解析錯誤,例如缺少大括號和分號。
使用 print_r() 和 file_put_contents() 函式將陣列寫入 PHP 檔案
該方法可以建立一個陣列,並使用 print_r() 函式返回該陣列,並使用 file_put_content() 函式將該陣列載入到檔案。print_r() 函式將要列印的陣列和布林值作為引數。布林值確定是否將返回陣列。預設值為 false。file_put_contents() 函式將檔案路徑作為第一個引數。第二個引數是要載入到檔案中的內容。
例如,建立一個關聯陣列。關聯陣列是鍵是字串而不是數字的陣列的型別。將鍵建立為 name,age 和 bike。將值 Jack、24 和 fireblade 指定為鍵的值。將陣列分配給變數 $b。使用 print_r() 函式並獲取引數 $b 和布林值 true。然後,編寫 file_put_contents() 函式,並指定檔案 filename.txt 作為第一個引數,並指定上方的 print_r() 函式作為第二個引數。
在下面的示例中,print_r() 函式返回陣列的資訊。它不會在網頁上列印專案。將在列印陣列的根目錄中建立一個檔案 filename.txt。請檢視 PHP 手冊中的 print_r() 以瞭解有關該功能的更多資訊。
示例程式碼:
# php 7.*
<?php
$b = array (
    'name' => 'Jack', 
    'age' => 24, 
    'bike' => 'fireblade');
file_put_contents('filename.txt', print_r($b, true));
?>
輸出:
Array
(
 [name] => Jack
 [age] => 24
 [bike] => fireblade
)
使用 fopen()、print_r() 和 fwrite() 函式將陣列寫入 PHP 檔案
我們可以使用 fwrite() 方法將陣列寫入檔案。此方法使用 fopen() 函式在目錄中建立可寫檔案。我們可以建立一個陣列,並使用 print_r() 函式返回該陣列,並使用 fwrite() 函式將該陣列寫入檔案。print_r() 函式將要列印的陣列和布林值作為引數。
使用 fopen() 函式以 w 作為第二個引數建立檔案 file.txt。將 fopen() 函式分配給變數 $fp。使用 print_r() 函式並獲取引數 $b 和布林值 true。然後,編寫 fwrite() 函式,並提供變數 $fp 作為第一個引數,並提供 print_r() 函式作為第二個引數。
在下面的示例中,fopen() 函式在根目錄中建立檔案 file.txt。使用 fwrite() 函式將該陣列寫入檔案。檢視 PHP 手冊,以瞭解有關 fwrite() 函式的更多資訊。
示例程式碼:
#php 7.x
<?php
$b = array (
    'name' => 'Harry', 
    'age' => 22, 
    'bike' => 'hayabusa');
$fp = fopen('file.txt', 'w');
fwrite($fp, print_r($b, true));
?>
輸出:
Array
(
 [name] => Harry
 [age] => 22
 [bike] => hayabusa
)
使用 fopen(),var_export() 和 fwrite() 函式將陣列寫入檔案
在此方法中,我們使用 var_export() 函式而不是 print_r() 函式來返回需要列印的陣列。var_export() 函式將要列印的陣列和布林值作為引數,就像 print_r() 函式一樣。我們使用 fopen() 函式在目錄中建立一個可寫檔案。我們以陣列和 var_export() 函式為引數,使用 fwrite() 函式將陣列寫入檔案。var_export() 函式返回指定引數的有效 PHP 程式碼。
例如,編寫 fwrite() 函式並提供變數 $fp 作為第一個引數,並在上方提供 var_export() 函式作為第二個引數。提供變數 $b 和布林值 true 作為 var_export() 函式的引數。
在下面的示例中,var_export() 函式在結構上產生了不同的輸出,而不是 print_r() 函式。var_export() 函式列印的陣列是有效的 PHP 程式碼,可以與 print_r() 函式生成的輸出不同地執行。請檢視 PHP 手冊以瞭解 var_export() 函式。
程式碼示例:
#php 7.x
<?php
$b = array (
    'name' => 'James', 
    'age' => 27, 
    'bike' => 'Yamaha R1');
$fp = fopen('file.txt', 'w');
fwrite($fp, var_export($b, true));
fclose($fp);
?>
輸出 :
array (
 'name' => 'James',
 'age' => 27,
 'bike' => 'Yamaha R1',
)
Subodh is a proactive software engineer, specialized in fintech industry and a writer who loves to express his software development learnings and set of skills through blogs and articles.
LinkedIn相關文章 - PHP File
- 如何在 PHP 中寫入檔案
- PHP 中的 require_once 與 include
- 在 PHP 中獲取當前目錄名稱和路徑
- 在 PHP 中建立和獲取 tmpfile 的路徑
- 在 PHP 中移動檔案到一個資料夾
- 使用 PHP_EOL 在 PHP 中設定行尾
