转换数组.txt文件 [英] convert array into .txt file

查看:200
本文介绍了转换数组.txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

可能重复:结果
  数组转换为CSV


如何数组转换成txt文件?

这是我的数组:

 阵列

    [OrderList_RetrieveByContactResult] =>排列
        (
            [订单明细] =>排列
                (
                    [0] =>排列
                        (
                            [ENTITYID] => 156456
                            [订单ID] => 110631
                            [ORDERNAME] => testing2
                            [statusTypeId] => 15656
                            [国家code => AU
                            [订单类型] => 2
                            [invoiceNumber] => 1001
                            [invoiceDate] => 2010-10-19T00:00:00
                            [userID_AssignedTo] => 245454
                            [shippingAmount] => 8.95
                            [shippingTaxRate] => 0
                            [shippingAttention] => tttesst
                            [shippingInstructions] =>这是一条测试
                            [shippingOptionId] => 50161
                            [折扣codeID => 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
                            [删除] =>
                            [产品] =>排列
                                (
                                    [产品] =>排列
                                        (
                                            的[ProductID => 455
                                            [产品code =>
                                            [产品描述] => TESTTEST
                                            [单位] => 3
                                            [单价] => 44.95
                                            [unitTaxRate] => 0
                                            [totalProductPrice] => 134.85
                                            [产品名称] =>皮肤支持平板电脑
                                        )
                                )                            [地址] =>排列
                                (
                                    [联系地址] =>排列
                                        (
                                            [addressTypeID] => 8
                                            [addressLine1] =>宿雾
                                            [城市] =>市
                                            [邮编code => 6000
                                            [状态] =>宿雾
                                            [国家code => PH
                                        )
                                )                        )                    [1] =>排列
                        (
                            [ENTITYID] => 315456
                            [订单ID] => 321321
                            [ORDERNAME] =>测试
                            [statusTypeId] => 3213
                            [国家code => AU
                            [订单类型] => 2
                            [invoiceNumber] => 1002
                            [invoiceDate] => 2010-10-19T00:00:00
                            [userID_AssignedTo] => 11711
                            [shippingAmount] => 8.95
                            [shippingTaxRate] => 0
                            [shippingAttention] =>
                            [shippingInstructions] =>
                            [shippingOptionId] => 50161
                            [折扣codeID => 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
                            [删除] =>
                            [产品] =>排列
                                (
                                    [产品] =>排列
                                        (
                                            的[ProductID => 11564
                                            [产品code =>
                                            [产品描述] =>
                                            [单位] => 10
                                            [单价] => 39.95
                                            [unitTaxRate] => 0
                                            [totalProductPrice] => 399.5
                                            [产品名称] =>痤疮结算凝胶
                                        )                                )                            [地址] =>排列
                                (
                                    [联系地址] =>排列
                                        (
                                            [addressTypeID] => 8
                                            [addressLine1] =>宿务市
                                            [城市] =>宿雾
                                            [邮编code => 6000
                                            [状态] =>
                                            [国家code => PH
                                        )                                )                        )                )        ))


解决方案

如果您希望它是可分析的,你可以使用


  • var_export - 输出或返回一个字符串可解析重新变
  • 的presentation

示例

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


如果你想让它看起来像你的问题,你通过 TRUE 作为第二个参数为的print_r

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


修改你要数组转化为一个CSV文件中的注释中提到。这是比较困难的。你的数组是嵌套的,而一个CSV文件基本上是一个单一的维数组,例如

 阵列(的k1= GT;'V1',的k2= GT;'V2',...,'千牛'=>'VN');

将相当于CSV文件,其中第一行是数组键和第二行中的值。

  K1; K2; ...; KN
V1; V2; ...; VN

单变暗阵列通常是另一个数组里面,让你有一个集合是这样的:

 阵列(
   阵列(的k1= GT;'V1',的k2= GT;'V2',...,'千牛'=>'VN'),
   阵列(的k1= GT;'V1',的k2= GT;'V2',...,'千牛'=>'VN')
);

在这里,你必须先拿到钥匙,然后从阵列为后续线路,例如只获取的值。

  K1; K2; ...; KN
V1; V2; ...; VN
V1; V2; ...; VN

问题是,你的阵列是嵌套更深。你会在哪里放的地址的阵列?唯一可行的办法是去阵列上,只取那些包含一个标量型按键和价值观,任何线性嵌套数组,例如

 阵列(
   阵列(;,的k2= GT'V1';'V2',...,'千牛'=>的k1= GT阵列('SKN'= GT;'的svn')),
   阵列(;,的k2= GT'V1';'V2',...,'千牛'=>的k1= GT阵列('SKN'= GT;'的svn')),
);

已被变成

  K1; K2; ...; SKN
V1; V2; ...; SVN
V1; V2; ...; SVN

在此情况下,明确心不是,这里是一个例子发生的事情,其中​​:

幸运的是,这个可以用迭代器来实现的:

  $ ORDERDETAILS = $阵列['OrderList_RetrieveByContactResult'] ['订单明细'];
的foreach($ ORDERDETAILS为$关键=> $细节){    $ =迭代器新RecursiveIteratorIterator(
            新RecursiveArrayIterator($详细信息));    如果($键=== 0){
        $键=阵列();
        的foreach($迭代器为$ K => $ V){
            $键[] = $ K表;
        }
        回声破灭(';',$键);
    }    $值=阵列();
    的foreach($迭代器为$ VAL){
        $值[] = $ VAL;
    }
    回声PHP_EOL,爆(';',$值);
}

以上code应该输出所描述的格式。请参见 http://www.ideone.com/I70dD 一个例子。

要写这一个文件,你可以组合这一个字符串变量,然后的file_put_contents 这一次全部或者你使用一个文件指针,它由一行行写。如果你不破灭,但遍历 $扁平数组,你也可以使用 fputcsv

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
                                        )

                                )

                        )

                )

        )

)

解决方案

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:

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.

这篇关于转换数组.txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆