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
                )

        )
)

I have this type of PHP array, want to write this array in sample.ini file and read that array in same format.I have written this array using this link this is write data in file but when read data from file like $ini_array = parse_ini_file("sample.ini"); and when i print that array then it will show me as,

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
...

So can anybody help me how can i get back array in the same format when i have written in file?

Community
  • 1
  • 1
jugni
  • 113
  • 1
  • 13
  • 1
    Hmmm maybe the `.ini` format isn't really suited for this. Try to make a JSON file `file_put_contents('file.json', json_encode($array));`, to retrieve it: `$array = json_decode(file_get_contents('file.json'));` – HamZa May 02 '13 at 09:39
  • 1
    `.ini` files may have only two levels; you can specify entries and groups. Use another storage format as suggested above. [YAML](http://en.wikipedia.org/wiki/Yaml) would a good solution too. – feeela May 02 '13 at 09:43
  • when we write multi times then it will override or insert as many as time we have inserted? – jugni May 02 '13 at 09:45
  • @jugni I saw `.ini`files used to store *configuration* variables. So I personally don't think it's suited to store other things than that especially not arrays. I suggest to take another format. – HamZa May 02 '13 at 09:49
  • @ HamZa DzCyberDeV i have used the json file and it is working fine but i hjvae doute about when i want to update dat at that time override data or insert again in the same file? – jugni May 02 '13 at 09:51
  • like i have write array of 15 value after that i have only 10 value in array so i want to write that 10 value in file but it should be update file value i mean whaterver i will write last that value should be remain in file.so it is possible? – jugni May 02 '13 at 09:53
  • @jugni Well that was a *basic* example. [file_put_contents()](http://php.net/manual/en/function.file-put-contents.php) has the **LOCK_EX** flag to lock the file. Updating means read+write. – HamZa May 02 '13 at 09:55

1 Answers1

1

if you are going to read this array in php you can just use something like this

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

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

and then just inclure your file and be happy

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;
}

Pros:

  • fast, especially when your file doesn't change frequently and you have some opcode cacher, e.g. XCache or APC or else

Cons:

  • not secure. The content may be changed to any code and it will be executed with webserver rights (or with logged in user if you use it as a CLI)

Actually we use this solution on high load cause it cost-effective and we have pretty good security to do not worry about unauthorized access. You may try but this is not the best practice but the fastest way to do the job.

pinepain
  • 12,453
  • 3
  • 60
  • 65