2017-06-15 100 views
-1

我需要登錄到一個文件中的陣列格式如下:-PHP-登錄請求和響應頭

`Array 
(
    [Date] => Thu, 15 Jun 2017 13:06:37 GMT 
    [Server] => Apache/2.4.18 (Win32) OpenSSL/1.0.2f PHP/5.6.18 
    [X-Powered-By] => PHP/5.6.18 
    [Content-Length] => 7790 
    [Content-Type] => application/json; charset=utf-8 
)` 

我該怎麼辦呢?

+0

你在登錄?你如何記錄?你現在有什麼?出了什麼問題? – Matthijs

回答

0

我知道你在尋找是file_put_contents:

int file_put_contents (string $filename , mixed $data [, int $flags = 0 [, resource $context ]]) 

非常易於使用。然而,似乎傳遞一個數組不會保留鍵,我會使用json_encode($ data)來保持結構完整。

file_put_contents($文件名,json_encode($數據))

是我所得到的日誌文件爲例:

{"[Date]":"Thu, 15 Jun 2017 13:06:37 GMT","[Server]":"Apache\/2.4.18 (Win32) OpenSSL\/1.0.2f PHP\/5.6.18","[X-Powered-By]":"PHP\/5.6.18","[Content-Length]":"7790","[Content-Type]":"application\/json; charset=utf-8"} 

這種方法的優點是你可以很容易地重新轉換成從日誌文件中的數組, 如果你想。

嗯,發現了另一個更簡單的方法:

file_put_contents ($filename , print_r($data, true)); 

其中保留原來的格式,甚至將它們放入該文件後。

例:

Array 
(
    [Date] => Thu, 15 Jun 2017 13:06:37 GMT 
    [Server] => Apache/2.4.18 (Win32) OpenSSL/1.0.2f PHP/5.6.18 
    [X-Powered-By] => PHP/5.6.18 
    [Content-Length] => 7790 
    [Content-Type] => application/json; charset=utf-8 
) 

http://php.net/manual/fr/function.file-put-contents.php