Convert array to .txt file

Possible duplicate:
Convert array to csv

Hi,

How to convert an array to a .txt file?

This is my array:

Array ( [OrderList_RetrieveByContactResult] => Array ( [OrderDetails] => Array ( [0] => Array ( [entityId] => 156456 [orderId] => 110631 [orderName] => testing2 [statusTypeId] => 15656 [countryCode] => AU [orderType] => 2 [invoiceNumber] => 1001 [invoiceDate] => 2010-10-19T00:00:00 [userID_AssignedTo] => 245454 [shippingAmount] => 8.95 [shippingTaxRate] => 0 [shippingAttention] => tttesst [shippingInstructions] => this a test [shippingOptionId] => 50161 [discountCodeId] => 0 [discountRate] => 0 [totalOrderAmount] => 143.8 [directDebitTypeId] => 0 [directDebitDays] => 0 [isRecur] => [nextInvoiceDate] => 0001-01-01T00:00:00 [endRecurDate] => 0001-01-01T00:00:00 [cycleTypeID] => 1 [createDate] => 2010-10-19T05:40:00 [lastUpdateDate] => 2010-10-19T05:40:00 [deleted] => [products] => Array ( [Product] => Array ( [productId] => 455 [productCode] => [productDescription] => testtest [units] => 3 [unitPrice] => 44.95 [unitTaxRate] => 0 [totalProductPrice] => 134.85 [productName] => Skin Support Tablets ) ) [addresses] => Array ( [Address] => Array ( [addressTypeID] => 8 [addressLine1] => Cebu [city] => City [zipcode] => 6000 [state] => cebu [countryCode] => PH ) ) ) [1] => Array ( [entityId] => 315456 [orderId] => 321321 [orderName] => testing [statusTypeId] => 3213 [countryCode] => AU [orderType] => 2 [invoiceNumber] => 1002 [invoiceDate] => 2010-10-19T00:00:00 [userID_AssignedTo] => 11711 [shippingAmount] => 8.95 [shippingTaxRate] => 0 [shippingAttention] => [shippingInstructions] => [shippingOptionId] => 50161 [discountCodeId] => 0 [discountRate] => 0 [totalOrderAmount] => 408.45 [directDebitTypeId] => 0 [directDebitDays] => 0 [isRecur] => [nextInvoiceDate] => 0001-01-01T00:00:00 [endRecurDate] => 0001-01-01T00:00:00 [cycleTypeID] => 1 [createDate] => 2010-10-08T18:40:00 [lastUpdateDate] => 2010-10-19T18:36:00 [deleted] => [products] => Array ( [Product] => Array ( [productId] => 11564 [productCode] => [productDescription] => [units] => 10 [unitPrice] => 39.95 [unitTaxRate] => 0 [totalProductPrice] => 399.5 [productName] => Acne Clearing Gel ) ) [addresses] => Array ( [Address] => Array ( [addressTypeID] => 8 [addressLine1] => Cebu City [city] => Cebu [zipcode] => 6000 [state] => [countryCode] => PH ) ) ) ) ) ) 
+4
source share
3 answers

If you want it to be disassembled, you can use

  • var_export - var_export or returns a syntax string representation of a variable

Example

 file_put_contents('array.txt', var_export($array, TRUE)); 

If you want this to be similar to your question, you pass TRUE as the second parameter to print_r :

 file_put_contents('array.txt', print_r($array, TRUE)); 

EDIT you mentioned in the comments that you want the array to be converted to a CSV file. This is harder. Your array is nested, while the CSV file is basically one small array, for example

 array('k1' => 'v1', 'k2' => 'v2', ... , 'kn' => 'vn'); 

will be equivalent to a CSV file, where the first lines are the keys of the array and the second line are the values.

 k1; k2; ...; kn v1; v2; ...; vn 

Single dims arrays are usually located inside another array, so you have a collection like this:

 array( array('k1' => 'v1', 'k2' => 'v2', ... , 'kn' => 'vn'), array('k1' => 'v1', 'k2' => 'v2', ... , 'kn' => 'vn') ); 

Here you need to first get the keys, and then extract only the values ​​from the array for subsequent lines, for example.

 k1; k2; ...; kn v1; v2; ...; vn v1; v2; ...; vn 

The problem is that your array is nested even deeper. Where would you put the address array? The only possible approach would be to go through the array and extract only those keys and values ​​that contain the scalar type, and linearize any nested arrays, for example.

 array( array('k1' => 'v1', 'k2' => 'v2', ... , 'kn' => array( 'skn' => 'svn' ) ), array('k1' => 'v1', 'k2' => 'v2', ... , 'kn' => array( 'skn' => 'svn' ) ), ); 

need to turn into

 k1; k2; ...; skn v1; v2; ...; svn v1; v2; ...; svn 

If this is not clear, here is an illustration of what happens where:

Flattening a Multidimensional Array for use in a CSV file

Fortunately, this can be achieved using iterators:

 $orderDetails = $array['OrderList_RetrieveByContactResult']['OrderDetails']; foreach( $orderDetails as $key => $details ) { $iterator = new RecursiveIteratorIterator( new RecursiveArrayIterator($details)); if($key === 0) { $keys = array(); foreach($iterator as $k => $v) { $keys[] = $k; } echo implode(';', $keys); } $values = array(); foreach($iterator as $val) { $values[] = $val; } echo PHP_EOL, implode(';', $values); } 

The above code should output the described format. See http://www.ideone.com/I70dD for an example.

To write this to a file, you can collect it in a string variable and then file_put_contents all at once, or you use a pointer file and write it line by line. If you are not implode , but iterating over the $flattened , you can also use fputcsv .

+10
source

If you want to save the variable in a text file, you can use serialize() / unserialize() to do this:

 $data = array(1, 2, 3, ''); $toBeSaved = serialize($data); file_put_contents('somefile.txt', $toBeSaved); 

And restore it ...

 $toBeRestored = file_get_contents('somefile.txt'); $data = unserialize($toBeRestored); 
+7
source

Perhaps you want fputcsv ?

+1
source

All Articles