Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3236728
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:39:19+00:00 2026-05-17T17:39:19+00:00

Possible Duplicate: Convert array into csv Hi, How to convert array into .txt file?

  • 0

Possible Duplicate:
Convert array into csv

Hi,

How to convert array into .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
                                        )

                                )

                        )

                )

        )

)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T17:39:20+00:00Added an answer on May 17, 2026 at 5:39 pm

    If you want it to be parseable, you can use

    • var_export — Outputs or returns a parsable string representation of a variable

    Example

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

    If you want it to look like in your question, you pass TRUE as the second param 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 transformed to a CSV file. This is more difficult. Your array is nested, while a CSV file is basically a single dim array, e.g.

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

    would be equivalent to a CSV file where the first rows is the array keys and the second row the values.

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

    The single dims arrays are usually 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’d have to get the keys first and then fetch only the values from the array for the subsequent lines, e.g.

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

    The problem is, your array is nested even deeper. Where would you put the address array? The only feasible approach would be to go over the array and fetch only those keys and values that contain a scalar type and linearize any nested arrays, e.g.

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

    has to be turned into

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

    In case this isnt clear, here is an illustration what goes where:

    Flattening a Multidimensional Array for use in a CSV file

    Luckily, this can be achieved with 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 either assemble this to a string variable and then file_put_contents it all at once or you use a filepointer and write it line by line. If you dont implode it but iterate over the $flattened array, you can also use fputcsv.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How can I convert my java program to an .exe file ?
Possible Duplicate: .NET String to byte Array C# How do I convert String to
Possible Duplicate: How do I calculate someone's age in C#? Maybe this could be
Possible Duplicate: Convert std::string to const char* or char* is there any to get
Possible Duplicate: Convert HTML + CSS to PDF with PHP? I was searching for
Possible Duplicate: Convert a String In C++ To Upper Case Hi, I need a
Possible Duplicate: Convert a number range to another range, maintaining ratio So I have
Possible Duplicate: Convert Month Number to Month Name Function in SQL Hi .... how
Possible Duplicate: Convert.ToInt32() a string with Commas i have a value in the label
Possible Duplicate: Convert std::string to const char* or char* Simple question but I'm new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.