2016-10-02 70 views
1

我有一個2維數組,我想保存到一個文件。文件中的每一行都應該是由選項卡分隔的項目的詳細信息。我會告訴你我的意思。無論如何,我有一個函數將數組拆分成一個字符串並保存到一個文件中,但它並不真正起作用。它給了我一個字符串轉換錯誤的數組。好了,這裏是我的數組:如何分割一個二維數組以保存到文件

{ 
    [0]=> array(8) { 
    ["title"]=> string(20) "The Boys in the Boat" 
    ["author"]=> string(18) "Daniel James Brown" 
    ["isbn"]=> string(10) "067002581X" 
    ["hardcover"]=> string(5) "19.99" 
    ["hc-quantity"]=> int(2) 
    ["softcover"]=> string(5) "16.99" 
    ["sc-quantity"]=> string(1) "9" 
    ["e-book"]=> string(6) "16.99 " 
    } 
    [1]=> array(8) { 
    ["title"]=> string(33) "Harry Potter and the Cursed Child" 
    ["author"]=> string(40) "J. K. Rowling, Jack Thorne, John Tiffany" 
    ["isbn"]=> string(10) "1338099133" 
    ["hardcover"]=> string(5) "18.95" 
    ["hc-quantity"]=> string(2) "25" 
    ["softcover"]=> string(5) "17.98" 
    ["sc-quantity"]=> string(1) "0" 
    ["e-book"]=> string(6) "17.98 " 
    } 
    [2]=> array(8) { 
    ["title"]=> string(10) "Just Mercy" 
    ["author"]=> string(15) "Bryan Stevenson" 
    ["isbn"]=> string(10) "0812994520" 
    ["hardcover"]=> string(5) "17.50" 
    ["hc-quantity"]=> string(1) "8" 
    ["softcover"]=> string(5) "16.25" 
    ["sc-quantity"]=> string(2) "10" 
    ["e-book"]=> string(6) "16.25 " 
    } 
    [3]=> array(8) { 
    ["title"]=> string(13) "Me Before You" 
    ["author"]=> string(10) "Jojo Moyes" 
    ["isbn"]=> string(10) "0670026603" 
    ["hardcover"]=> string(5) "18.95" 
    ["hc-quantity"]=> string(1) "2" 
    ["softcover"]=> string(5) "17.50" 
    ["sc-quantity"]=> string(1) "1" 
    ["e-book"]=> string(6) "17.25 " 
    } 
    [4]=> array(8) { 
    ["title"]=> string(24) "A Thousand Splendid Suns" 
    ["author"]=> string(15) "Khaled Hosseini" 
    ["isbn"]=> string(10) "1594489505" 
    ["hardcover"]=> string(5) "19.00" 
    ["hc-quantity"]=> string(1) "7" 
    ["softcover"]=> string(5) "15.50" 
    ["sc-quantity"]=> string(1) "4" 
    ["e-book"]=> string(6) "14.95 " 
    } 
    [5]=> &array(8) { 
    ["title"]=> string(19) "The Wright Brothers" 
    ["author"]=> string(16) "David McCullough" 
    ["isbn"]=> string(10) "1476728742" 
    ["hardcover"]=> string(5) "21.95" 
    ["hc-quantity"]=> string(1) "3" 
    ["softcover"]=> string(5) "18.95" 
    ["sc-quantity"]=> string(1) "3" 
    ["e-book"]=> string(6) "18.95 " 
    } 
} 

這裏是我的功能:

function saveorder($file,$arr){ 
    @ $fp = fopen($file, 'w'); 

    flock($fp, LOCK_EX); 
    $string = ''; 
    foreach($arr as $key => $result) 
    $string .= "$key: $result \n"; 

    if (!$fp) { 
    echo "<p>Your order could not be processed at this time.</p>"; 
    }else{ 
    fwrite($fp, $string); 
    flock($fp, LOCK_UN); 
    fclose($fp); 

    echo("<p>Order written to $file file.</p>"); 
    }  
} 

如果它的工作,例如第一線,會讀這樣的事情:「在船上的孩子們\ t作者\精裝\ t「,類似的東西。你能幫忙嗎?

+0

數組如何顯示? – Thomas

回答

1
function saveorder($file,$arr) 
{ 
    $text = []; 
    foreach($arr as $row) { 
     $text[]= implode("\t", $row); 
    } 
    file_put_contents($file, implode("\n", $text)); 
} 
相關問題