3
Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => >2013-05-02 09:46:26<
                    [1] => >441656721714 <441656721714><
                    [2] => >United Kingdom FIX United Kingdom - Fixed Landline<
                    [3] => >ANSWERED<
                    [4] => >441752387112<
                    [5] => >132<
                    [6] => >441<
                    [7] => >441656721714<
                    [8] => >0.0099<
                )

            [1] => Array
                (
                    [0] => 2013-05-02 09:46:26
                    [1] => 441656721714 <441656721714>
                    [2] => United Kingdom FIX United Kingdom - Fixed Landline
                    [3] => ANSWERED
                    [4] => 441752387112
                    [5] => 132
                    [6] => 441
                    [7] => 441656721714
                    [8] => 0.0099
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [0] => >2013-05-02 09:46:26<
                    [1] => >441656721714 <441656721714><
                    [2] => >  <
                    [3] => >ANSWERED<
                    [4] => >448455774074<
                    [5] => >132<
                    [6] => ><
                    [7] => >441656721714<
                    [8] => >0.0<
                )

            [1] => Array
                (
                    [0] => 2013-05-02 09:46:26
                    [1] => 441656721714 <441656721714>
                    [2] =>   
                    [3] => ANSWERED
                    [4] => 448455774074
                    [5] => 132
                    [6] => 
                    [7] => 441656721714
                    [8] => 0.0
                )

        )
)

我有这种类型的 PHP 数组,想在 sample.ini 文件中写入这个数组并以相同的格式读取该数组。我已经使用这个链接编写了这个数组,这是在文件中写入数据,但是当从文件中读取数据$ini_array = parse_ini_file("sample.ini");时,当我打印该数组,然后它将显示为,

Array
(
    [0] => Array
        (
            [0] => >2013-05-02 09:46:26<
            [1] => >441656721714 <441656721714><
            [2] => >United Kingdom FIX United Kingdom - Fixed Landline<
            [3] => >ANSWERED<
            [4] => >441752387112<
            [5] => >132<
            [6] => >441<
            [7] => >441656721714<
            [8] => >0.0099<
            [9] => >2013-05-02 09:46:26<
            [10] => >441656721714 <441656721714><
            [11] => >  <
            [12] => >ANSWERED<
            [13] => >448455774074<
            [14] => >132<
            [15] => ><
            [16] => >441656721714<
            [17] => >0.0<
            [18] => >2013-05-02 08:59:01<
            [19] => >441217720398 <441217720398><
            [20] => >United Kingdom FIX United Kingdom - Fixed Landline<
            [21] => >ANSWERED<
            [22] => >441752387112<
            [23] => >82<
            [24] => >441<
            [25] => >441217720398<
            [26] => >0.00615<
            [27] => >2013-05-02 08:59:01<
            [28] => >441217720398 <441217720398><
            [29] => >  <
            [30] => >ANSWERED<
            [31] => >448455774074<
            [32] => >82<
            [33] => ><
            [34] => >441217720398<
            [35] => >0.0<
            [36] => >2013-05-02 08:36:14<
            [37] => >Anonymous <Anonymous><
            [38] => >United Kingdom FIX United Kingdom - Fixed Landline<
            [39] => >ANSWERED<
            [40] => >441752387112<
            [41] => >378<
            [42] => >441<
            [43] => >Anonymous<
            [44] => >0.02835<
            [45] => >2013-05-02 08:36:14<
            [46] => >Anonymous <Anonymous><
            [47] => >  <
            [48] => >ANSWERED<
            [49] => >448455774074<
            [50] => >378<
            [51] => ><
            [52] => >Anonymous<
            [53] => >0.0<
            [54] => >2013-05-01 21:54:20<
            [55] => >443303500040 <443303500040><
            [56] => >United Kingdom FIX United Kingdom - Fixed Landline<
            [57] => >ANSWERED<
            [58] => >442920337371<
            [59] => >1535<
            [60] => >442<
            [61] => >443303500040<
            [62] => >0.117683<
            [63] => >2013-05-01 21:53:37<
            [64] => >443303500040 <443303500040><
            [65] => >United Kingdom MOB United Kingdom - Mobile - H3G<
            [66] => >ANSWERED<
            [67] => >447878354165<
            [68] => >20<
            [69] => >447878<
            [70] => >443303500040<
            [71] => >0.004067<
            [72] => >2013-05-01 21:53:27<
            [73] => >443303500040 <443303500040><
            [74] => >United Kingdom FIX United Kingdom - Fixed Landline<
            [75] => >BUSY<
            [76] => >442920337371<
            [77] => >0<
            [78] => >442<
            [79] => >443303500040<
            [80] => >0.0<
            [81] => >2013-05-01 21:53:17<
            [82] => >443303500040<
            [83] => >United Kingdom NGN Aloha GB - On Net<
            [84] => >NO ANSWER<
            [85] => >443303500067<
            [86] => >0<
            [87] => >44330350<
            [88] => >443303500040<
            [89] => >0.0<
            [90] => >2013-05-01 21:52:42<
            [91] => > <443303500040><
            [92] => >United Kingdom NGN Aloha GB - On Net<
            [93] => >ANSWERED<
            [94] => >443303500067<
            [95] => >12<
            [96] => >44330350<
            [97] => >443303500040<
            [98] => >0.0013<
            [99] => >2013-05-01 21:51:44<
            [100] => >443303500040 <443303500040><
            [101] => >United Kingdom FIX United Kingdom - Fixed Landline<
            [102] => >BUSY<
            [103] => >442920337371<
            [104] => >0<
            [105] => >442<
            [106] => >443303500040<
            [107] => >0.0<
            [108] => >2013-05-01 21:51:20<
            [109] => >443303500040 <443303500040><
            [110] => >United Kingdom FIX United Kingdom - Fixed Landline<
            [111] => >BUSY<
            [112] => >442920337371<
            [113] => >0<
            [114] => >442<
            [115] => >443303500040<
            [116] => >0.0<
            [117] => >2013-05-01 21:50:56<
            [118] => > <443303500040><
            [119] => >Peru MOB Peru - Mobile - Telefonica<
            [120] => >ANSWERED<
            [121] => >51971812978<
            [122] => >5<
            [123] => >51971<
            [124] => >443303500040<
            [125] => >0.0051<
            [126] => >2013-05-01 21:40:53<
            [127] => >51971812978 <51971812978><
            [128] => >United Kingdom NGN Aloha GB - On Net<
            [129] => >BUSY<
            [130] => >443303506050<
            [131] => >0<
            [132] => >44330350<
            [133] => >51971812978<
            [134] => >0.0<
            [135] => >2013-05-01 21:31:01<
            [136] => >443303500040 <443303500040><
            [137] => >United Kingdom FIX United Kingdom - Fixed Landline<
            [138] => >NO ANSWER<
            [139] => >442920337371<
            [140] => >0<
            [141] => >442<
            [142] => >443303500040<
            [143] => >0.0<
            [144] => >2013-05-01 19:34:26<
            [145] => >443303500040 <443303500040><
            [146] => >United Kingdom FIX United Kingdom - Fixed Landline<
            [147] => >ANSWERED<
            [148] => >442920337371<
            [149] => >1314<
            [150] => >442<
            [151] => >443303500040<
            [152] => >0.10074<
        )

    [1] => Array
        (
            [0] => 2013-05-02 09:46:26
            [1] => 441656721714 <441656721714>
            [2] => United Kingdom FIX United Kingdom - Fixed Landline
            [3] => ANSWERED
            [4] => 441752387112
...

那么,当我写入文件时,任何人都可以帮助我如何以相同的格式取回数组?

4

1 回答 1

1

如果你要在 php 中读取这个数组,你可以使用类似这样的东西

$content = '<?php' . PHP_EOL;
$content .= '$arr = ' . var_export($your_array, true) . ';' .PHP_EOL;

file_put_contents('file.php', $content);

然后只需包含您的文件并感到高兴

function getArray() {
    include 'file.php';
    if(!isset($arr) {
        // ooops! something goes wrong
        return array(); // throw exception or process as you want such sitation
    }
    return $arr;
}

优点:

  • 快速,尤其是当您的文件不经常更改并且您有一些操作码缓存器时,例如 XCache 或 APC 或其他

缺点:

  • 不安全。内容可以更改为任何代码,并且将使用网络服务器权限执行(或者如果您将其用作 CLI,则使用登录用户)

实际上,我们在高负载下使用这个解决方案,因为它具有成本效益,而且我们有很好的安全性,不用担心未经授权的访问。您可以尝试,但这不是最佳做法,而是完成这项工作的最快方法。

于 2013-05-02T19:33:01.187 回答