Will Barangi Will Barangi - 1 year ago 106
PHP Question

How to split a two dimensional array to save to a file

I have a 2 dimensional array that I want to save to a file. Every line in the file should be the details of an item seperated by a tab. I will show you what I mean in a few. Anyway, I have a function that splits the array into a string and saves to a file but it's not really working. It's giving me an array to string conversion error. Okay, so here is my array:

[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 "

And here is my function:

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>";
fwrite($fp, $string);
flock($fp, LOCK_UN);

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

If it's working, the first line for example, would read something like this: "The boys in the boat \t author\ hardcover\t", something like that. Can you please help?

Answer Source
function saveorder($file,$arr)
   $text = [];
   foreach($arr as $row) {
       $text[]= implode("\t", $row);
    file_put_contents($file, implode("\n", $text));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download